Entity Framework 6 adds additional foreign key with underscore

c# ef-migrations entity-framework-6 mysql

Question

I'm having a trouble with Entity Framework 6.

The Migration generates additional foreignkey with underscore.

I have a table for Person and PersonRelative

Person Table

  • Id
  • Name

PersonRelative Table

  • Id

  • PersonId

  • RelativeId (Person type)

  • Relationship (Additional Table, to determine what is the relationship of the Person to its Relative.)

Using code first the Entity Framework migration adds an additional foreign key for PersonId and it's a Person_Id.

So basically, the PersonRelative table has:

  • Id
  • PersonId
  • RelativeId
  • Relationship
  • Person_Id

This is the generated code:

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);

My Model for 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; }

}
1
0
7/18/2018 9:13:25 AM

Popular Answer

I manage to fixed my issue by adding this code (see code snippet below) to my DbContext OnModelCreating.

With the help of this documentation: https://docs.microsoft.com/en-us/ef/ef6/modeling/code-first/fluent/relationships#configuring-a-composite-foreign-key

Thanks to @Giovanni for pointing me to a right direction.

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

My Person Relative Class:

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; }

}

My Person Class:

public class Person
{
   public int Id { get; set; } 
   public string Name{ get; set; }
   public ICollection<PersonRelative> PersonRelatives {get;set;}
}
0
7/19/2018 9:21:45 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow