Do I need to create a DBSet for every table? So that I can persist child entities?

笑着哭i 提交于 2020-01-06 14:15:33

问题


My EF5 model

public Layout()
{
   public int Id {get;set}
   public BindingList<Column> Columns {get;set}
}
public Column()
{
   public int Id {get;set}
   public string Name {get;set}

   [Required]
   [ForeignKey("LayoutId")
   public virtual Layout Layout {get;set}
   public int LayoutId {get;set}

}

In my context I only need to specify

DBSet<Layout> Layouts {get;set}

I dont need to specify DBSet Columns for the database to create the way I want it with both tables.

Also I am happy in my code to only ever access columns via the layout object However i cant figure out how to persist the layout object with its columns correctly.

This question here describes how to save parent objects with children, but it would require me to create a DBSet Columns property in my context. Do I really have to do that?


回答1:


It seems I can get by without having a DBSet in the context if instead of using

Context.Columns.Remove(col)

I use

Layout.Columns.Remove(col)
Context.Entry(col).State = EntityState.Deleted;


来源:https://stackoverflow.com/questions/20233994/do-i-need-to-create-a-dbset-for-every-table-so-that-i-can-persist-child-entitie

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!