简体   繁体   English

如何创建对一个表的多个表引用 - 实体框架

[英]How to create multiple table references to one table - Entity framework

在此输入图像描述

We have a requirement that several tables need to be referred in a one table as shown in the diagram. 我们要求在一个表中引用几个表,如图所示。

Please note that the diagram is NOT the correct DB model but just represent what our requirement. 请注意,该图表不是正确的数据库模型,只是代表了我们的要求。 Can you suggest a proper way to implement above in SQL Server DB and Entity Framework 6 ? 您能否在SQL Server DB和Entity Framework 6建议一种正确的实现方式?

Example: A sales order (in SalesOrder table) can have multiple files and those uploaded file details will be stored in UploadedFile table. 示例:销售订单(在SalesOrder表中)可以包含多个文件,这些上载的文件详细信息将存储在UploadedFile表中。 Likewise OrderTable and Invoice too. 同样也是OrderTable和Invoice。

So we need to have a proper DB model with FK relationships between FileUpload table with each other related table. 因此,我们需要在FileUpload表与彼此相关的表之间建立一个具有FK关系的正确数据库模型。

Note: All tables PKs are auto-increment int values and we may need add more entities (tables) in future 注意:所有表PK都是自动增加int值,我们可能需要在将来添加更多实体(表)

In order to provide you an alternative, as you said you are using Entity Framework, here is a sample of Code First implementation done on C#. 为了向您提供替代方案,正如您所说的使用实体框架,这里是在C#上完成的Code First实现的示例。 You can create and update the schema via Package Manager Console migrations, within Visual Studio. 您可以在Visual Studio中通过程序包管理器控制台迁移创建和更新架构。 I have used the Fluent API in order to define the relationships, as this is recommended over the alternative. 我已经使用了Fluent API来定义关系,因为这是建议的替代方案。

public class SampleContext : DbContext
{
    public SampleContext()
        : base("name=YourConnection")
    {
    }

    public DbSet<SalesOrder> SalesOrders { get; set; }
    public DbSet<CreditOrder> CreditOrders { get; set; }
    public DbSet<Invoice> Invoices { get; set; }
    public DbSet<UploadedFile> UploadedFiles { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<SalesOrder>()
            .HasKey(so => so.Id);

        modelBuilder.Entity<CreditOrder>()
            .HasKey(co => co.Id);

        modelBuilder.Entity<Invoice>()
            .HasKey(i => i.Id);

        modelBuilder.Entity<UploadedFile>()
            .HasKey(u => u.Id);

        modelBuilder.Entity<UploadedFile>()
            .HasRequired(u => u.SalesOrder)
            .WithMany(s => s.UploadedFiles)
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<UploadedFile>()
            .HasRequired(u => u.CreditOrder)
            .WithMany(c => c.UploadedFiles)
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<UploadedFile>()
            .HasRequired(u => u.Invoice)
            .WithMany(c => c.UploadedFiles)
            .WillCascadeOnDelete(false);

        modelBuilder.Entity<UploadedFile>()
            .Property(uf => uf.Id)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

        modelBuilder.Entity<SalesOrder>()
            .Property(so => so.Id)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

        modelBuilder.Entity<CreditOrder>()
            .Property(co => co.Id)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

        modelBuilder.Entity<Invoice>()
            .Property(i => i.Id)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

        base.OnModelCreating(modelBuilder);
    }
}

// Collections of navigation properties should be included in classes for a one-to-many relationship

public class SalesOrder
{
    public int Id { get; set; }
    public string MyColumn { get; set; }
    public IList<UploadedFile> UploadedFiles { get; set; }
}

public class CreditOrder
{
    public int Id { get; set; }
    public string MyColumn { get; set; }
    public IList<UploadedFile> UploadedFiles { get; set; }
}

public class Invoice
{
    public int Id { get; set; }
    public string MyColumn { get; set; }
    public IList<UploadedFile> UploadedFiles { get; set; }
}

public class UploadedFile
{
    public int Id { get; set; }
    public SalesOrder SalesOrder { get; set; }
    public CreditOrder CreditOrder { get; set; }
    public Invoice Invoice { get; set; }
    public string FilePath { get; set; }
    public string FileType { get; set; }
}

public class SalesOrder
{
    public int Id { get; set; }
    public string MyColumn { get; set; }
    public IList<UploadedFile> UploadedFiles { get; set; }
}

public class CreditOrder
{
    public int Id { get; set; }
    public string MyColumn { get; set; }
    public IList<UploadedFile> UploadedFiles { get; set; }
}

public class Invoice
{
    public int Id { get; set; }
    public string MyColumn { get; set; }
    public IList<UploadedFile> UploadedFiles { get; set; }
}

public class UploadedFile
{
    public int Id { get; set; }
    public SalesOrder SalesOrder { get; set; }
    public CreditOrder CreditOrder { get; set; }
    public Invoice Invoice { get; set; }
    public string FilePath { get; set; }
    public string FileType { get; set; }
}

Before Reading My Answer take notice in the below information: 在阅读我的答案之前,请注意以下信息:

A better answer can be found at this previous post at Foreign Key to multiple tables . 可以在以前的多个表的外键上找到更好的答案。


My Answer: 我的答案:

Refer here for more information from the Creating Tables documentation off of the MDSN page. 有关MDSN页面上的创建表文档的更多信息,请参阅此处

The design I went for gave each table there own id as the Primary Key . 我去的设计给每个表都有自己的id作为主键 Then I used the UploadedFile table to add references to each of these tables in the shape of Foreign Keys . 然后我用UploadedFile的表将引用添加到每个表的外键的形状。

I have created a few dummy scripts that might help you create these tables. 我创建了一些可能帮助您创建这些表的虚拟脚本。 Please let me know if this helps. 请让我知道这可不可以帮你。 Thank you! 谢谢!

SQL Scripts: SQL脚本:

SalesOrder Table: SalesOrder表:

Create Table dbo.SalesOrder(
SalesOrderID int not null, identity primary key,
--enter whatever other columsn you have here
)

CreditOrder Table: CreditOrder表:

Create Table sbo.CreditOrder(
CreditOrderID int not null, identity primary key,
--enter whatever other columsn you have here
)

Invoice Table: 发票表:

Create Table dbo.Invoice(
InvoiceID int not null, identity primary key,
--enter whatever other column you have here
)

UploadedFile Table: UploadedFile表:

Create table dbo.UploadedFile(
UploadFileID int not null identity primary key,
SalesOrderID int null Foreign Key References SalesOrder(SalesOrderID),
CreditOrderID int null Foreign Key References CreditOrder(CreditOrderID),
InvoiceID int null Foreign Key References CreditOrder(InvoiceID),
--enter whatever other columns you have here
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM