C# 实体框架代码首次迁移后的外来列 [表(“IpForeclosureActionHeaders”)] 公共类止赎行动标题:FullAuditedEntity { //为了清晰起见,删除了其他属性 [外国密钥(“银行ID”)] 公共虚拟银行{get;set;} 公共虚拟整数BankId{get;set;} [外国钥匙(“转让银行ID”)] 公共虚拟银行已分配给银行{get;set;} 公共虚拟int赋值数据库ID{get;set;} } [表(“IpBanks”)] 公共类银行:全面审计 { //为了清晰起见,删除了其他属性 公共虚拟ICollection止赎ActionHeaders{get;set;} 公共虚拟ICollection AssignedToBankForeclosureActionHeaders{get;set;} }
迁移文件:C# 实体框架代码首次迁移后的外来列 [表(“IpForeclosureActionHeaders”)] 公共类止赎行动标题:FullAuditedEntity { //为了清晰起见,删除了其他属性 [外国密钥(“银行ID”)] 公共虚拟银行{get;set;} 公共虚拟整数BankId{get;set;} [外国钥匙(“转让银行ID”)] 公共虚拟银行已分配给银行{get;set;} 公共虚拟int赋值数据库ID{get;set;} } [表(“IpBanks”)] 公共类银行:全面审计 { //为了清晰起见,删除了其他属性 公共虚拟ICollection止赎ActionHeaders{get;set;} 公共虚拟ICollection AssignedToBankForeclosureActionHeaders{get;set;} },c#,entity-framework,code-first,entity-framework-migrations,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; }
[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" },
});
}
}
public override void Up()
{
创建表(
“dbo.IpForeclosureActionHeaders”,
c=>新的
{
Id=c.Int(可为空:false,标识:true),
BankId=c.Int(可为空:false),
AssignedToBankId=c.Int(可为空:false),
Bank_Id=c.Int(),
Bank_Id1=c.Int(),
},
注释:新词典
{
{
“DynamicFilter\u止赎行动标题\u SoftDelete”,
“EntityFramework.DynamicFilters.DynamicFilterDefinition”
},
})
.PrimaryKey(t=>t.Id)
.ForeignKey(“dbo.IpBanks”,t=>t.Bank\u 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\u Id)
.Index(t=>t.Bank_Id1)
}
公共覆盖无效向下()
{
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”,新[]{“Bank_Id1”});
DropIndex(“dbo.IpForeclosureActionHeaders”,新[]{“Bank_Id”});
DropIndex(“dbo.IpForeclosureActionHeaders”,新[]{“AssignedToBankId”});
DropIndex(“dbo.IpForeclosureActionHeaders”,新[]{“BankId”});
DropTable(“dbo.IpForeclosureActionHeaders”,
removedAnnotations:新词典
{
{“DynamicFilter\u止赎ActionHeader\u SoftDelete”,“EntityFramework.DynamicFilter.DynamicFilterDefinition”},
});
}
}
Q:你能告诉我为什么它会在
IpForeclosureActionHeaders
表上创建Bank\u Id
和Bank\u Id 1
列吗?因为我为这些列命名为BankId
和AssignedToBankId
。我怎样才能避免呢?提前谢谢 您可以阅读此线程-通常情况相同:
InverseProperty
将帮助您避免这种不必要的引用
解决方案:
[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" },
});
}
}