どの追加と削除が行えます。多対多の関係団体の枠組みにより企ーション-キーとは何ですか。

StackOverflow https://stackoverflow.com/questions/1077554

質問

てみました:

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();
}

を使う際にRemove()なしで呼び出しを添付の前例外がスローされますが関係に削除されません。

役に立ちましたか?

解決

うようになります:

User user = new User {UserId = 20};
e.AttachTo("Users", user);
Role role = e.Roles.FirstOrDefault();
role.Users.Add(user);
e.SaveChanges();

を見たいのですスタブ法などのユーザー)ではなくEntityKeys.

この ブログ 詳細はスタブ企業です。

武器agiは、dexで下がらないboxerぐ

アレックス

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top