我需要为每张桌子创建一个DBSet吗?所以我可以坚持孩子实体? [英] Do I need to create a DBSet for every table? So that I can persist child entities?

查看:143
本文介绍了我需要为每张桌子创建一个DBSet吗?所以我可以坚持孩子实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的EF5模型

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}

我不需要为数据库指定DBSet列来创建我的方式想要它与两个表。

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?

推荐答案

如果不使用DBSet,

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

Context.Columns.Remove(col)

我使用

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

这篇关于我需要为每张桌子创建一个DBSet吗?所以我可以坚持孩子实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆