[英]Foreign Key already exists For Relation Error in EF6
I am using Entity Framework 6.4.x with Npgsql and code first approach.我正在使用带有 Npgsql 和代码优先方法的 Entity Framework 6.4.x。
I created the following entity:我创建了以下实体:
public class UserBankAccountTransaction
{
public long PayerBankAccountId { get; set; }
[ForeignKey(nameof(PayerBankAccountId))]
public virtual UserBankAccount PayerBankAccount { get; set; }
public long PayeeBankAccountId { get; set; }
[ForeignKey(nameof(PayeeBankAccountId))]
public virtual UserBankAccount PayeeBankAccount { get; set; }
[Required]
public int Amount { get; set; }
}
That is all, now when I do Add-Migration
it creates a following migration:就是这样,现在当我执行Add-Migration
时,它会创建以下迁移:
CreateTable(
"public.UserBankAccountTransactions",
c => new
{
Id = c.Long(nullable: false, identity: true),
PayerBankAccountId = c.Long(nullable: false),
PayeeBankAccountId = c.Long(nullable: false),
Amount = c.Int(nullable: false),
CreatedDate = c.DateTime(nullable: false),
ModifiedDate = c.DateTime(nullable: false),
})
.PrimaryKey(t => t.Id)
.ForeignKey("public.UserBankAccounts", t => t.PayeeBankAccountId, cascadeDelete: true)
.ForeignKey("public.UserBankAccounts", t => t.PayerBankAccountId, cascadeDelete: true)
.Index(t => t.PayerBankAccountId)
.Index(t => t.PayeeBankAccountId);
As you can see above in the automatically generated migration that "public.UserBankAccounts"
is a foreign key being generated two times with same name.正如您在上面自动生成的迁移中看到的那样, "public.UserBankAccounts"
是一个外键,被生成了两次,名称相同。
Due to this When I Update-Migration
I get this error:因此,当我Update-Migration
时,我收到此错误:
constraint "FK_public.UserBankAccountTransactions_public.UserBankAccounts_P" for relation "UserBankAccountTransactions" already existsa
Problem问题
Isn't there a way to add more than one References/ForeignKeys to same table in EF6?没有办法在 EF6 的同一张表中添加多个引用/外键吗?
Postgres has limit for maximum length of identifier - 63 bytes , so both of your foreign keys end up with the same name - FK_public.UserBankAccountTransactions_public.UserBankAccounts_P
. Postgres 对标识符的最大长度有限制 - 63 字节,因此您的两个外键最终都具有相同的名称 - FK_public.UserBankAccountTransactions_public.UserBankAccounts_P
。
So you need to find a way to specify FK name.所以你需要找到一种方法来指定 FK 名称。 Not sure that it can be done via attribute/fluent api (at least I was not able to find it) but as workaround you should be able to specify it via name
parameter of TableBuilder<TColumns>.ForeignKey
method in the generated migration (which is usually not a good thing to do):不确定它是否可以通过属性/流利的 api (至少我找不到它)来完成,但作为解决方法,您应该能够通过生成的迁移中的TableBuilder<TColumns>.ForeignKey
方法的name
参数来指定它(其中通常不是一件好事):
.ForeignKey("public.UserBankAccounts", t => t.PayeeBankAccountId, cascadeDelete: true, name: "FK_UBAT_UBA_PayeeBankAccountId" )
.ForeignKey("public.UserBankAccounts", t => t.PayerBankAccountId, cascadeDelete: true, name: "FK_UBAT_UBA_PayerBankAccountId")
I have same issue but with Sql server
and I solved it like this:我有同样的问题,但使用Sql server
,我解决了这个问题:
public class UserBankAccountTransaction
{
[ForeignKey(nameof(PayerBankAccount))]
public long PayerBankAccountId { get; set; }
public virtual UserBankAccount PayerBankAccount { get; set; }
[ForeignKey(nameof(PayeeBankAccount))]
public long PayeeBankAccountId { get; set; }
public virtual UserBankAccount PayeeBankAccount { get; set; }
[Required]
public int Amount { get; set; }
}
public class UserBankAccount
{
//other properties
[InverseProperty(nameof(UserBankAccountTransaction.PayerBankAccount))]
public UserBankAccountTransaction PayerUserBankAccountTransaction { get; set; }
//or public List<UserBankAccountTransaction> PayerUserBankAccountTransactions { get; set; }
[InverseProperty(nameof(UserBankAccountTransaction.PayeeBankAccount))]
public UserBankAccountTransaction PayeeUserBankAccountTransaction { get; set; } // or list ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.