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

时间:2018-10-23 13:07:32

标签: c# asp.net sql-server database entity-framework-6

enter image description here

我们有一个要求,即在一个表中需要引用多个表,如图所示。

请注意,该图不是正确的数据库模型,仅代表我们的要求。您能建议在SQL Server数据库和Entity Framework 6中实现上述方法的正确方法吗?

示例:一个销售订单(在SalesOrder表中)可以有多个文件,这些上载的文件详细信息将存储在UploadedFile表中。同样,OrderTable和发票也是如此。

因此,我们需要一个正确的数据库模型,该模型具有FileUpload表与其他相关表之间的FK关系。

注意:所有表PK都是自动递增的int值,将来我们可能需要添加更多实体(表)

2 个答案:

答案 0 :(得分:1)

在阅读“我的答案”之前,请注意以下信息:

在上一篇Foreign Key to multiple tables上可以找到更好的答案。


我的答案:

请参阅here,以获取MDSN页面上的“创建表”文档中的更多信息。

我进行的设计为每个表提供了自己的ID,作为主键。然后,我使用UploadedFile表以外键的形式添加对这些表的引用。

我创建了一些虚拟脚本,可以帮助您创建这些表。请让我知道这可不可以帮你。谢谢!

SQL脚本:

SalesOrder表:

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

信用订单表:

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

发票表:

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

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
)

答案 1 :(得分:1)

为了向您提供其他选择,就像您说的那样,您正在使用Entity Framework,下面是在C#上完成的Code First实现的示例。您可以在Visual Studio中通过程序包管理器控制台迁移来创建和更新架构。我已使用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; }
}