Salve a tutti.
Ho il seguente problema: nel mio model ho due classi, Utente e Ruolo che sono legati da una relazione N a M.
Finchè lavoro con entità "attached" (ovvero col DbSet<Utente> o DbSet<Ruolo>) tutto funziona benissimo.
Se però creo una classe a runtime e poi provo ad usare il metodo "Attach", non riesco a far funzionare la relazione coi ruoli.
Per esempio se faccio questo:
Ruolo r = context.Ruoli.AsNoTracking().FirstOrDefault(r=>r.Id == 1);
Utente u = context.Utenti.AsNoTracking().FirstOrDefault(u => u.Id == 1);
u.nome = "nome";
....
u.Ruoli.Add(ruolo);
contex.UtenteRepository.Update(u);
il codice aggiorna correttamente le proprietà semplici (come il nome), però non mi crea/modifica nulla nella tabella di relazione con i ruoli.
Il metodo "update" (generico) è il seguente:
public virtual void Update(T entity)
{
Context.Set<T>().Attach(entity);
Context.Entry<T>(entity).State = EntityState.Modified;
Context.ChangeTracker.DetectChanges();
Stopwatch sw = new Stopwatch();
sw.Start();
ObjectContext ctx = ((IObjectContextAdapter)Context).ObjectContext;
StringBuilder sb = new StringBuilder();
sb.AppendLine(DateTime.Now.ToString());
sb.AppendFormat("Aggiornata una entità di tipo: {0}\n", typeof(T).Name);
foreach (ObjectStateEntry entry in ctx.ObjectStateManager.GetObjectStateEntries(EntityState.Modified
| EntityState.Added
| EntityState.Deleted))
{
switch (entry.State)
{
case EntityState.Added:
sb.AppendFormat("Added {0} {1}", entry.IsRelationship, entry.CurrentValues);
foreach (string propertyName in entry.GetModifiedProperties())
{
DbDataRecord original = entry.OriginalValues;
string oldValue = original.GetValue(original.GetOrdinal(propertyName)).ToString();
CurrentValueRecord current = entry.CurrentValues;
string newValue = current.GetValue(current.GetOrdinal(propertyName)).ToString();
if (oldValue != newValue)
{
sb.AppendFormat("added {0}: {1} => {2}\n", propertyName, oldValue, newValue);
}
}
break;
case EntityState.Deleted:
sb.AppendFormat("Deleted {0} {1}", entry.IsRelationship, entry.CurrentValues);
foreach (string propertyName in entry.GetModifiedProperties())
{
DbDataRecord original = entry.OriginalValues;
string oldValue = original.GetValue(original.GetOrdinal(propertyName)).ToString();
CurrentValueRecord current = entry.CurrentValues;
string newValue = current.GetValue(current.GetOrdinal(propertyName)).ToString();
if (oldValue != newValue)
{
sb.AppendFormat("deleted {0}: {1} => {2}\n", propertyName, oldValue, newValue);
}
}
break;
case EntityState.Modified:
foreach (string propertyName in entry.GetModifiedProperties())
{
DbDataRecord original = entry.OriginalValues;
string oldValue = original.GetValue(original.GetOrdinal(propertyName)).ToString();
CurrentValueRecord current = entry.CurrentValues;
string newValue = current.GetValue(current.GetOrdinal(propertyName)).ToString();
if (oldValue != newValue)
{
sb.AppendFormat("{0}: {1} => {2}\n", propertyName, oldValue, newValue);
}
}
break;
default:
break;
}
}
Context.SaveChanges();
sb.AppendFormat("Tempo impiegato: {0} ms\n", sw.ElapsedMilliseconds);
File.AppendAllText("modifiche.log", sb.ToString());
sw.Stop();
sw = null;
}
Dove sto sbagliando?