2
Ich habe versucht:Hinzufügen/Entfernen von Viele-zu-viele-Beziehung mit Entity-Framework nach Entitätsschlüssel?
using (Entities e = new Entities())
{
EntityKey key = new EntityKey("Entities.Users", "UserId", 20);
User user = new User { EntityKey = key};
Role role = e.Roles.FirstOrDefault();
//role.Users.Attach(user); //throws (when uncommented...) InvalidOperationException:
//The object being attached to the source object is not attached to the same ObjectContext as the source object.
role.Users.Add(user); //throws InvalidOperationException too:
//The object cannot be added to the ObjectStateManager because it already has an EntityKey. Use ObjectContext.Attach to attach an object that has an existing key.
e.SaveChanges();
}
Beim Versuch() zu verwenden, entfernen ohne Aufruf an, bevor keine Ausnahme ausgelöst wird, aber Beziehung nicht gelöscht.
Ist "Entities" in Ihrem Beispiel vom Typ System.Data.Entity.DbContext? – jaybro
Nachforschung: Wenn "AttachTo" verwendet wird, muss "Entities" ein ObjectContext sein. – jaybro
LOL wahrscheinlich, das ist ein alter Post ... Tag hinzugefügt – Shimmy