2017-06-01 85 views
0

在实体框架6代码中,我可以向我的C#对象模型中添加哪些属性以用于1-1父/子关系而底层的SQL表关系是1到多?在EF6中,如何在1到多个SQL表关系的顶部关联1#关系的c#模型

数据在SQL Server中为特定客户通用于所有的客户数据和数据之间的分割2012

它有三个实体,一个WarehouseBase基础机构,WarehouseCustom客户特定数据和客户的实体。

C#的对象:

Warehouse 
{ 
    public Guid ID {get;set;} 
    public string Name {get;set;} 
    public string Note {get;set;} //customer specific data 
} 

Customer 
{ 
    public Guid ID {get;set;} 
    public string Name {get;set;} 
} 

客户用户可以使用这个工作流程:

  1. 客户在浏览器中查看仓库XYZ。它应该返回仓库XYZ共享数据(ID,名称)和客户特定数据(注)。
  2. 客户编辑说明,并点击保存按钮
  3. 注意应保存到数据库

的管理员将编辑并保存WarehouseBase数据(名称),而不是客户的具体数据。

SQL表:

WarehouseBase 
    ID : Guid 
    Name: Nvarchar (255) 

WarehouseCustom 
    ID : Guid 
    WarehouseBaseID: GUID 
    CustomerID : GUID 
    Note : Nvarchar (255) 

Customer 
    ID : Guid 
    Name : Nvarchar (255) 

该系统具有使用Web浏览器前端连接到服务器的WebAPI多个不同的并发用户。服务器使用EF6/C#来访问SQL Server。

可以为此C#模型添加哪些属性?

已经看到:

  1. 一对多的关系:http://www.entityframeworktutorial.net/code-first/configure-one-to-many-relationship-in-code-first.aspx
  2. 如何查询一对多和加载特定的子数据:Entity Framework include filter child collection
+0

我们的应用程序将有50多种这些类型的基础/自定义实体;所以避免为每个实体手动编码2个查询加载和1个查询保存会有所帮助。 – testx

+0

对于新客户,备注的子对象可能不存在。 – testx

+0

用户将首先通过仓库名称(基本实体数据)进行搜索以查找并加载仓库名称。通过仓库记录首先进行搜索或链接表比添加2个查询来读取仓库和1个查询来更新仓库注释增加了更多的复杂性。 – testx

回答

0

的C#应该是

Warehouse { 
    public Guid ID {get;set;} 
    public string Name {get;set;} 

    public ICollection<WarehouseCustom> CustomersNotes {get; set;} 
} 

Customer { 
    public Guid ID {get;set;} 
    public string Name {get;set;} 

    public ICollection<WarehouseCustom> WarehouseNotes {get; set;} 
} 

WarehouseCustom { 
    public Guid ID {get;set;} 
    public string Note {get;set;} //customer specific data 

    public virtual Warehouse {get; set;} 
    public virtual Customer {get; set;} 
} 

因此,一位顾客可以在战争中访问他的所有笔记仓库和仓库管理员可以访问仓库中的所有注释。

为了防止客户在仓库的客户备注中导航,您必须注意实现数据。

using (AppContext ctx = new AppContext()) { 
    return ctx.Customers. 
     Include(x => x.WarehouseNotes.Select(y => y.Wharehouse)). 
     Where(x => x.ID == oneID). 
     ToList(); 
} 

即:不要使用懒惰加载。

在代码示例中,您只能加载客户应该看到的内容。

理想情况下tou应该使用DTO类型。

顺便说一句:您不需要仓库自定义ID(如果您可以为一个仓库创建多个客户注释,则需要输入ID)。您可以使用由仓库编号和客户编号组成的复杂关键字。

恕我直言,这最后一个约束将强制你命名1对1的关系。

+0

如果客户有仓库备注但不适用于客户的情况想要找到没有备注的仓库,然后向仓库添加备注。 – testx

+0

谢谢tschmit007 – testx