2016-02-09 56 views
0

我有一個「嵌套的」實體框架模型結構。這裏是我的模型:如何使用實體框架更改嵌套模型?

class Parent 
{ 
    public int ParentId {get;set;} 
    virtual ICollection<Child> children {get;set;} 
} 

class Child 
{ 
    public int ChildId {get;set;} 
    public Parent Parent {get;set;} 
    virtual ICollection<Account> accounts {get;set;} 
} 

class Account 
{ 
    public int AccountId {get;set;} 
    public Child Child {get;set;} 
    public string SomeProperty {get;set;} 
} 

class DbContext 
{ 
    public DbSet<Parent> parents {get;set;} 
    public DbSet<Child> children {get;set;} 
    public DbSet<Account> accounts {get;set;} 
} 

如何更改爲屬於某一父以最小的數據庫查詢所有賬戶的Account.SomeProperty?

回答

0

這個怎麼樣

var db = new DbContext(); 
var accounts = db.accounts.Where(a => a.Child.Parent.ParentId == parentId); 
foreach (var account in accounts) 
{ 
    account.SomeProperty = ...; 
} 
db.SaveChanges(); 

它會產生只有一個查詢(必要的連接)來檢索「所有帳戶屬於某個父」。超出您的控制範圍會生成多少個更新語句。