如何使用EF Code First将一张桌子与许多家长联系起来 [英] How to associate one table to many parents using EF Code First

查看:137
本文介绍了如何使用EF Code First将一张桌子与许多家长联系起来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个域模型,需要多个表格才能被多个可能的父表格引用。像你这样的东西可能有一个表来存储笔记或文件,这些笔记和/或文件可以与不同的父实体相关联。不能将相同的文件或注释与多个所有者相关联,而文件表中的10行可能相同,其中3个可能由客户表中的行拥有,其中3个可能是由Orders表中的行拥有,其中4个可能由Person表中的行拥有。



拥有的表都具有虚拟ICollections子表。



当我运行我的模型通过代码首次迁移进行初始迁移,并查看生成的流畅代码,这些表的表定义包括多个id列回溯到源表,以便有一个列customer_id,person_id等(每个可能的拥有实体一个。这似乎不是很对的有更好的使用CodeFirst在EF中建模这个方法吗?像在表中提供关于所有实体的提示的辨别器列一样?



谢谢
-MrB

解决方案

如果您在笔记和所有可以注释的实体之间创建多对多关系,那么您将拥有每个所有类型的连接表和一个漂亮干净的注释表。创建多对多关系的最简单的方法是覆盖 DbContext.OnModelCreating 方法。



您的DbContext:

  public class MyDatabase:DbContext 
{
//受保护的方法

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

modelBuilder.Entity< Customer>()。HasMany(x => x.Notes).WithMany();
modelBuilder.Entity< Order>()。HasMany(x => x.Notes).WithMany();
}

public MyDatabase():base(MyDatabase)
{
}

//属性

public DbSet< Customer>客户{get;组; }

public DbSet< Order>订单{get;组; }

public DbSet< Note>注意{get;组; }
}

这将产生以下表格:客户订单备注客户注释 OrderNotes ,最后两个为连接表, / p>

注意!使用这种解决方案,实体也可以共享笔记,例如客户和订单可以拥有相同的备注。


I am building a domain model that requires several tables to be able to be references by more than one possible parent table. Something like you might have a table to store notes or files and those notes and/or files can be associated with different parent entities. Not that the same "file" or "note" can be associate with multiple owners, but that of 10 rows in a "files" table, 3 of them might be owned by rows from a "Customer" table, 3 of them might be owned by rows from an "Orders" table, and 4 of them might be owned by rows from a "Person" table.

The owning tables all have virtual ICollections of the child tables.

When I ran my model through the code first migration for the initial migration, and looked at the generated fluent code, the table definition for these tables included multiple id columns that referred back to the source table, so that there would be a column "customer_id," "person_id," etc. (one for each possible "owning entity." This doesn't seem quite "right." Is there a better way to model this in EF with CodeFirst? Like being able to use a discriminator column in the child table that provides a "hint" about the owning entity?

Thanks, -MrB

解决方案

If you create a many-to-many relationship between notes and all entities that can have notes, you will have a junction table for each owning type and a nice and clean note table. The simplest way to create the many-to-many relationships is to override the DbContext.OnModelCreating method.

Your DbContext:

  public class MyDatabase : DbContext
  {
    // Protected methods

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
      base.OnModelCreating(modelBuilder);

      modelBuilder.Entity<Customer>().HasMany(x => x.Notes).WithMany();
      modelBuilder.Entity<Order>().HasMany(x => x.Notes).WithMany();
    }

    public MyDatabase() : base("MyDatabase")
    {
    }

    // Properties

    public DbSet<Customer> Customers { get; set; }

    public DbSet<Order> Orders { get; set; }

    public DbSet<Note> Notes { get; set; }
  }

This will produce the following tables: Customer, Orders, Notes, CustomerNotes and OrderNotes with the last two being junction tables with only two columns each.

NOTE! With this solution it is also possible for entities to share notes, e.g. a Customer and an Order can own the same Note.

这篇关于如何使用EF Code First将一张桌子与许多家长联系起来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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