简体   繁体   English

实体框架外键多表映射

[英]Entity Framework Foreign Key multiple table mapping

I have this Scenario: 我有这种情况:

public class Application
{
    [Key]
    public string Code { get; set; }
}

public class Table1
{
    [Key]
    public string Table1Code { get; set; }

    [Key, ForeignKey("ApplicationObject")]
    public string Application { get; set; }
    public virtual Application ApplicationObject { get; set; }
}

public class Table2
{
    [Key]
    public string Table2Code { get; set; }

    [Key, ForeignKey("ApplicationObject")]
    public string Application { get; set; }
    public virtual Application ApplicationObject { get; set; }
}

public class Table3
{
    [Key, ForeignKey("Table1Object")]
    public string Table1Code { get; set; }
    public virtual Table1 Table1Object { get; set; }

    [Key, ForeignKey("Table2Object")]
    public string Table2Code { get; set; }
    public virtual Table2 Table2Object { get; set; }

    [Key, ForeignKey("Table1Object")]
    public string ApplicationCodeTab1 { get; set; }

    [Key, ForeignKey("Table2Object")]
    public string ApplicationCodeTab2 { get; set; }
}

In the table1 and table2 the property applicationCode must be key because i can have the same code for different application. 在table1和table2中,属性applicationCode必须是键,因为我可以为不同的应用程序使用相同的代码。

In the Table3 i have referenced Table1 and Table2. 在表3中,我引用了表1和表2。 How Can I add Foreign Key for the ApplicationCode Property without duplicate the property? 如何在没有重复属性的情况下为ApplicationCode属性添加外键?

For Example: 例如:

public class Table3
{
    [Key, ForeignKey("Table1Object")]
    public string Table1Code { get; set; }
    public virtual Table1 Table1Object { get; set; }

    [Key, ForeignKey("Table2Object")]
    public string Table2Code { get; set; }
    public virtual Table2 Table2Object { get; set; }

    [Key]
    public string ApplicationCode { get; set; }
}

The ApplicationCode property in Table3 can rapresent the Foreign Key for Table1 and Table2 in same time? Table3中的ApplicationCode属性可以同时表示Table1和Table2的外键吗?

The key is to move the ForeignKey attribute to the navigation property and specify a the foreign key columns. 关键是将ForeignKey属性移动到导航属性并指定一个外键列。

Like this: 像这样:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace ConsoleApp6
{

    public class Application
    {
        [Key]
        public string Code { get; set; }
    }

    public class Table1
    {
        [Key]
        [Column(Order = 0)]
        public string Table1Code { get; set; }

        [Key, ForeignKey("ApplicationObject")]
        [Column(Order = 1)]
        public string Application { get; set; }
        public virtual Application ApplicationObject { get; set; }
    }

    public class Table2
    {
        [Key]
        [Column(Order = 0)]
        public string Table2Code { get; set; }

        [Key, ForeignKey("ApplicationObject")]
        [Column(Order = 1)]
        public string Application { get; set; }
        public virtual Application ApplicationObject { get; set; }
    }

    public class Table3
    {
        [Key]
        [Column(Order = 0)]
        public string Table1Code { get; set; }

        [Key]
        [Column(Order = 1)]
        public string Table2Code { get; set; }

        [Key]
        [Column(Order = 2)]
        public string ApplicationCode { get; set; }

        [ForeignKey("Table1Code,ApplicationCode")]
        public virtual Table1 Table1Object { get; set; }

        [ForeignKey("Table2Code,ApplicationCode")]
        public virtual Table2 Table2Object { get; set; }



    }
    class Db: DbContext
    {


        public DbSet<Table1> Table1 { get; set; }
        public DbSet<Table2> Table2 { get; set; }
        public DbSet<Table3> Table3 { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<System.Data.Entity.ModelConfiguration.Conventions.OneToManyCascadeDeleteConvention>();
            base.OnModelCreating(modelBuilder);
        }

    }
    class Program
    {

        static void Main(string[] args)
        {


            Database.SetInitializer(new DropCreateDatabaseAlways<Db>());

            using (var db = new Db())
            {
                db.Database.Log = m => Console.WriteLine(m);

                db.Database.Initialize(true);


            }

            Console.ReadKey();

        }
    }
}

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

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