1
    [Table("IpForeclosureActionHeaders")]
    public class ForeclosureActionHeader : FullAuditedEntity
    {
        // removed other properties for clarity

        [ForeignKey("BankId")]
        public virtual Bank Bank { get; set; }
        public virtual int BankId { get; set; }

        [ForeignKey("AssignedToBankId")]
        public virtual Bank AssignedToBank { get; set; }
        public virtual int AssignedToBankId { get; set; }

    }

    [Table("IpBanks")]
    public class Bank : FullAuditedEntity
    {
         // removed other properties for clarity

        public virtual ICollection<ForeclosureActionHeader> ForeclosureActionHeaders { get; set; }

        public virtual ICollection<ForeclosureActionHeader> AssignedToBankForeclosureActionHeaders { get; set; }
    }

Migration file :

public override void Up()
        {
            CreateTable(
                "dbo.IpForeclosureActionHeaders",
                c => new
                {
                    Id = c.Int(nullable: false, identity: true),

                    BankId = c.Int(nullable: false),
                    AssignedToBankId = c.Int(nullable: false),
                    Bank_Id = c.Int(),
                    Bank_Id1 = c.Int(),
                },
                annotations: new Dictionary<string, object>
                {
                    {
                        "DynamicFilter_ForeclosureActionHeader_SoftDelete",
                        "EntityFramework.DynamicFilters.DynamicFilterDefinition"
                    },
                })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.IpBanks", t => t.Bank_Id)
                 .ForeignKey("dbo.IpBanks", t => t.Bank_Id1)
                .ForeignKey("dbo.IpBanks", t => t.AssignedToBankId, cascadeDelete: false)
                .ForeignKey("dbo.IpBanks", t => t.BankId, cascadeDelete: false)
                 .Index(t => t.BankId)
                .Index(t => t.AssignedToBankId)
             .Index(t => t.Bank_Id)
             .Index(t => t.Bank_Id1)

        }

        public override void Down()
        {
            DropForeignKey("dbo.IpForeclosureActionHeaders", "BankId", "dbo.IpBanks");
            DropForeignKey("dbo.IpForeclosureActionHeaders", "AssignedToBankId", "dbo.IpBanks");
            DropForeignKey("dbo.IpForeclosureActionHeaders", "Bank_Id1", "dbo.IpBanks");
            DropForeignKey("dbo.IpForeclosureActionHeaders", "Bank_Id", "dbo.IpBanks");
            DropIndex("dbo.IpForeclosureActionHeaders", new[] { "Bank_Id1" });
            DropIndex("dbo.IpForeclosureActionHeaders", new[] { "Bank_Id" });
            DropIndex("dbo.IpForeclosureActionHeaders", new[] { "AssignedToBankId" });
            DropIndex("dbo.IpForeclosureActionHeaders", new[] { "BankId" });
            DropTable("dbo.IpForeclosureActionHeaders",
                removedAnnotations: new Dictionary<string, object>
                {
                    { "DynamicFilter_ForeclosureActionHeader_SoftDelete", "EntityFramework.DynamicFilters.DynamicFilterDefinition" },
                });
        }
    }

enter image description here

Q : Could you tell me why it creates Bank_Id and Bank_Id1 columns on the IpForeclosureActionHeaders table ? B'cose I have given name for those columns as BankId and AssignedToBankId. How can I avoid it ? Thanks in advance.

Sampath
  • 63,341
  • 64
  • 307
  • 441

1 Answers1

3

You can read this thread - it's generally the same situation: Why is EF code-first generating an extraneous foreign key column?

InverseProperty will help you to avoid this unnecessary reference.

Solution :

    [ForeignKey("BankId")]
    [InverseProperty("ForeclosureActionHeaders")]
    public virtual Bank Bank { get; set; }
    public virtual int BankId { get; set; }

    [ForeignKey("AssignedToBankId")]
    [InverseProperty("AssignedToBankForeclosureActionHeaders")]
    public virtual Bank AssignedToBank { get; set; }
    public virtual int AssignedToBankId { get; set; }
Community
  • 1
  • 1
MagisterCrazy
  • 225
  • 1
  • 10