1

我有两个表:EF6 +/7添加了哪些可以添加更新子表的方法?

public AdminTest() 
    { 
     this.AdminTestQuestions = new List<AdminTestQuestion>(); 
    } 
    public int AdminTestId { get; set; } 
    public string Title { get; set; }  
    public virtual ICollection<AdminTestQuestion> AdminTestQuestions { get; set; } 
} 

public partial class AdminTestQuestion 
{ 
    public int AdminTestQuestionId { get; set; } 
    public int AdminTestId { get; set; } 
    public System.Guid QuestionUId { get; set; } 
    public virtual AdminTest AdminTest { get; set; } 
} 

我使用下面的EF6代码中添加一个新的adminTest(其adminTestQuestions)到 数据库:

public async Task<IHttpActionResult> Post([FromBody]AdminTest adminTest) 
    { 
     db.AdminTests.Add(adminTest); 
     foreach (AdminTestQuestion adminTestQuestion in adminTest.AdminTestQuestions) 
     { 
      db.AdminTestQuestions.Add(adminTestQuestion); 
     } 
     await db.SaveChangesAsync(User, DateTime.UtcNow); 
     return Ok(adminTest); 
    } 

我有类似但更复杂的代码处理添加或删除adminTest问题的情况。我所有的代码都可以工作,但如果EF能够做我所需要的,而不是添加许多代码行,那将会非常好。

谁能告诉我,如果有到过EF6任何改变,或者如果任何更改计划EF7,将允许它

+1

[无尚的地位(http://data.uservoice.com/forums/72025-entity-framework-feature-suggestions/suggestions/1069431-merge - 方法 - 自动同步关系)和[仍然提议](http://entityframework.codeplex.com/workitem/864) – 2014-09-30 14:02:42

回答

-1

对他们的接缝已经增加了一些整洁的代码,添加主键EF7 github上指出实体。

但是它仍然不清楚在实体中是否会收集儿童的常见信息。 Git hub Entity Framework Design Meeting Notes

但是对于EF6,您可以使用Generic Repository来为您完成所有工作。 (因为你不能延伸的DbContext直接)

假设DB是一个的DbContext

,你可以用这个 - >:Accessing a Collection Through Reflection

然后从包含的ICollection <一个T类找到所有楼盘>和做对ICollection属性的项目的foreach然后执行db.Set.Add(proprietyChild)

这将消除总是重复相同的添加孩子到实体代码的需要。

有些人已经做了实现解决方案你:Automated updates of a graph of deached entities