c# - Entity Framework 代码优先迁移后的外来列

标签 c# entity-framework code-first entity-framework-migrations

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

迁移文件:

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

问:您能告诉我为什么它会在 IpForeclosureActionHeaders 表上创建 Bank_IdBank_Id1 列吗?因为我已将这些列命名为 BankIdAssignedToBankId。我怎样才能避免呢?提前致谢。

最佳答案

您可以阅读此线程 - 通常情况相同:Why is EF code-first generating an extraneous foreign key column?

InverseProperty 将帮助您避免这种不必要的引用。

解决方案:

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

关于c# - Entity Framework 代码优先迁移后的外来列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34023847/

相关文章:

c# - 如何使Entity Framework生成的类实现INotifyPropertyChanged?

c# - 在 Entity Framework 4.1 (CodeFirst) 中更新对子对象的引用

entity-framework - Entity Framework 代码优先方法获取错误 : "The underlying provider failed on Open"

.net - 自数据库创建以来,支持 <Database> 上下文的模型已发生更改

c# - 我如何通过单声道从 CPython 调用 C#?

c# - 相机行为

c# - Entity Framework 等效于ADO.Net的DataRow.HasErrors?

用于根据相关数据进行过滤的 iOS Core Data Predicate

c# - 将 Task 包装为 Task<TResult> 的最佳方法是什么

c# - 在 AIR native 扩展中包含外部 DLL