2011-08-15 5 views

答えて

0

のAdminRoleテーブルを更新したい な管理者の役割のAdminRole のように多くの関係だけに、多くのナビゲーションプロパティを更新する、方法

public void Update(T entity, params Expression<Func<T, object>>[] properties) 
{ 
    _dbSet.Attach(entity); 
    DbEntityEntry<T> entry = _context.Entry(entity); 
    foreach (var selector in properties) { entry.Property(selector).IsModified = true; } 
} 
//repo.Update(entity, e => e.Name, e => e.Description); 

の下で使用しますが、更新することができますリレーションシップを更新したい場合は、単純なトリックを使うことができます。

Admin admin = new Admin { Id = adminId }; 
context.Admins.Attach(admin); 
Role role = new Role { Id = roleId }; 
context.Roles.Attach(role); 
// Create new relation on attached entities 
admin.Roles.Add(role); 
context.SaveChanges(); 

あなたはこの試みることができる管理者と役割間の既存の関係を削除するには:

は、あなたがする必要がある管理者と役割の間の新しい関係を追加するには、両方のシナリオで

Admin admin = new Admin { Id = adminId }; 
Role role = new Role { Id = roleId }; 
// Simulate existing relation on detached entities 
admin.Roles.Add(role); 
context.Admins.Attach(admin); 
// Remove existing relation on attached entities 
admin.Roles.Remove(role); 
context.SaveChanges(); 

をあなただけの管理者と役割の鍵を知る必要があります。

注:これは実際の多対多(ジャンクション表でキーのみを含む)のシナリオです。

+0

SystemAdminエンティティ= base.GetByKey(admin.SAID); DbEntityEntry エントリ= base.DataContext.Entry(エンティティ); entity.AdminRoles.Clear(); entry.Collection(a => a.AdminRoles).CurrentValue = admin.AdminRoles; – user895367

関連する問題