首页 文章

实体框架6使用下划线添加额外的外键

提问于
浏览
0

我在使用Entity Framework 6时遇到了麻烦 .

迁移使用下划线生成其他外键 .

我有一个Person和PersonRelative表

Person Table

  • 同上

  • 姓名

PersonRelative Table

  • 同上

  • PersonId

  • RelativeId(人物类型)

  • Relationship(附加表,用于确定Person与其Relative的关系是什么 . )

首先使用代码,Entity Framework迁移为PersonId添加了一个额外的外键,它是一个Person_Id .

所以基本上, PersonRelative 表有:

  • 同上

  • PersonId

  • RelativeId

  • 关系

  • Person_Id

这是生成的代码:

CreateTable(
            "dbo.PersonRelatives",
            c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    Relationship = c.string(nullable: false),
                    PersonId= c.Int(nullable: false),
                    RelativeId= c.Int(nullable: false),
                    Person_Id= c.Int(),
                })
            .PrimaryKey(t => t.Id)
            .ForeignKey("dbo.Persons", t => t.PersonId, cascadeDelete: true)
            .ForeignKey("dbo.Persons", t => t.RelativeId, cascadeDelete: true)

            .ForeignKey("dbo.Persons", t => t.Person_Id)
            .Index(t => t.PersonId)
            .Index(t => t.RelativeId)
            .Index(t => t.Person_Id);

我的PersonRelative Entity模型:

public class PersonRelative
{
    public int Id { get; set; }

    public string Relationship{ get; set; }




    [ForeignKey("Person")]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int PersonId{ get; set; }
    public Person Person { get; set; }

    [ForeignKey("Relative")]
    public int RelativeId { get; set; }
    public Person Relative { get; set; }

}

2 回答

  • 0

    您可以尝试覆盖 DbContext 中的 OnModelCreating 并手动指定关系 .

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<PersonRelative>()
           .HasOne(p => p.Person)
           .HasForeignKey<int>(p => p.PersonId);          
    }
    

    在这里阅读更多关于键和关系的信息:https://docs.microsoft.com/en-us/ef/core/modeling/relational/fk-constraints

  • 0

    我设法通过将此代码(请参阅下面的代码段)添加到我的DbContext OnModelCreating来修复我的问题 .

    借助此文档:https://docs.microsoft.com/en-us/ef/ef6/modeling/code-first/fluent/relationships#configuring-a-composite-foreign-key

    感谢@Giovanni指出我正确的方向 .

    modelBuilder.Entity<Person>()
           .HasMany(a => a.PersonRelatives)
           .WithMany()
           .Map(a =>
           {
               a.MapLeftKey("PersonId");
               a.MapRightKey("RelativeId");
           });
    

    我的人员相对类:

    public class PersonRelative
    {
        public int Id { get; set; }
    
        public string Relationship{ get; set; }
    
    
    
    
        [ForeignKey("Person")]
        public int PersonId{ get; set; }
        public Person Person { get; set; }
    
        [ForeignKey("Relative")]
        public int RelativeId { get; set; }
        public Person Relative { get; set; }
    
    }
    

    我的人员类:

    public class Person
    {
       public int Id { get; set; } 
       public string Name{ get; set; }
       public ICollection<PersonRelative> PersonRelatives {get;set;}
    }
    

相关问题