同一个 table 上的多个外键。如何重新设计我的模型?
Multiple foreign keys on the same table. How to redesign my model?
我想使用 EF Core 创建一个简单的 MoneyT运行saction 模型,如下所示,我 运行 出现以下错误:
An exception of type 'System.InvalidOperationException' occurred in Microsoft.EntityFrameworkCore.dll but was not handled in user code: 'Cannot create a relationship between 'Member.MoneyTransactions' and 'MoneyTransaction.PaidBy' because a relationship already exists between 'Member.MoneyTransactions' and 'MoneyTransaction.ChargedBy'. Navigation properties can only participate in a single relationship. If you want to override an existing relationship call 'Ignore' on the navigation 'MoneyTransaction.PaidBy' first in 'OnModelCreating'.'
这是我到目前为止编写的代码:
public class Member
{
// ...
public string MoneyTransactionsId { get; set; }
public ICollection<MoneyTransaction> MoneyTransactions { get; set; } = new List<MoneyTransaction>();
// ...
}
public class MoneyTransaction
{
// ...
public string PaidById { get; set; }
public Member PaidBy { get; set; }
public string ChargedById { get; set; }
public Member ChargedBy { get; set; }
// ...
}
问题出现在 OnModelCreating:
protected override void OnModelCreating(ModelBuilder builder)
{
// ...
builder.Entity<Member>()
.HasMany(m => m.MoneyTransactions)
.WithOne(t => t.PaidBy)
.HasForeignKey(t => t.PaidById)
.OnDelete(DeleteBehavior.NoAction)
.HasPrincipalKey(m => m.Id);
builder.Entity<Member>()
.HasMany(m => m.MoneyTransactions)
.WithOne(t => t.ChargedBy)
.HasForeignKey(t => t.ChargedById)
.HasPrincipalKey(m => m.Id);
// ...
}
我不确定如何设计我的模型以使其仅通过 Fluent API 工作(不想在我的模型 类 中使用 DataAnnotations)。这可能是一个简单的解决方案,但不知何故我找不到任何有用的东西来重构我的模型以使其正常工作。
由于 Member
可以在一笔交易中既可以收费也可以付款,因此它应该有两个列表 MoneyTransaction
- 一个是他们收费的,一个是他们支付的 -
public class Member
{
// ...
public string MoneyTransactionsId { get; set; }
public ICollection<MoneyTransaction> ChargedTransactions { get; set; } = new List<MoneyTransaction>();
public ICollection<MoneyTransaction> PaidTransactions { get; set; } = new List<MoneyTransaction>();
// ...
}
然后你可以将它们配置为-
builder.Entity<Member>()
.HasMany(m => m.PaidTransactions)
.WithOne(t => t.PaidBy)
.HasForeignKey(t => t.PaidById)
.OnDelete(DeleteBehavior.NoAction)
.HasPrincipalKey(m => m.Id);
builder.Entity<Member>()
.HasMany(m => m.ChargedTransactions)
.WithOne(t => t.ChargedBy)
.HasForeignKey(t => t.ChargedById)
.HasPrincipalKey(m => m.Id);
我想使用 EF Core 创建一个简单的 MoneyT运行saction 模型,如下所示,我 运行 出现以下错误:
An exception of type 'System.InvalidOperationException' occurred in Microsoft.EntityFrameworkCore.dll but was not handled in user code: 'Cannot create a relationship between 'Member.MoneyTransactions' and 'MoneyTransaction.PaidBy' because a relationship already exists between 'Member.MoneyTransactions' and 'MoneyTransaction.ChargedBy'. Navigation properties can only participate in a single relationship. If you want to override an existing relationship call 'Ignore' on the navigation 'MoneyTransaction.PaidBy' first in 'OnModelCreating'.'
这是我到目前为止编写的代码:
public class Member
{
// ...
public string MoneyTransactionsId { get; set; }
public ICollection<MoneyTransaction> MoneyTransactions { get; set; } = new List<MoneyTransaction>();
// ...
}
public class MoneyTransaction
{
// ...
public string PaidById { get; set; }
public Member PaidBy { get; set; }
public string ChargedById { get; set; }
public Member ChargedBy { get; set; }
// ...
}
问题出现在 OnModelCreating:
protected override void OnModelCreating(ModelBuilder builder)
{
// ...
builder.Entity<Member>()
.HasMany(m => m.MoneyTransactions)
.WithOne(t => t.PaidBy)
.HasForeignKey(t => t.PaidById)
.OnDelete(DeleteBehavior.NoAction)
.HasPrincipalKey(m => m.Id);
builder.Entity<Member>()
.HasMany(m => m.MoneyTransactions)
.WithOne(t => t.ChargedBy)
.HasForeignKey(t => t.ChargedById)
.HasPrincipalKey(m => m.Id);
// ...
}
我不确定如何设计我的模型以使其仅通过 Fluent API 工作(不想在我的模型 类 中使用 DataAnnotations)。这可能是一个简单的解决方案,但不知何故我找不到任何有用的东西来重构我的模型以使其正常工作。
由于 Member
可以在一笔交易中既可以收费也可以付款,因此它应该有两个列表 MoneyTransaction
- 一个是他们收费的,一个是他们支付的 -
public class Member
{
// ...
public string MoneyTransactionsId { get; set; }
public ICollection<MoneyTransaction> ChargedTransactions { get; set; } = new List<MoneyTransaction>();
public ICollection<MoneyTransaction> PaidTransactions { get; set; } = new List<MoneyTransaction>();
// ...
}
然后你可以将它们配置为-
builder.Entity<Member>()
.HasMany(m => m.PaidTransactions)
.WithOne(t => t.PaidBy)
.HasForeignKey(t => t.PaidById)
.OnDelete(DeleteBehavior.NoAction)
.HasPrincipalKey(m => m.Id);
builder.Entity<Member>()
.HasMany(m => m.ChargedTransactions)
.WithOne(t => t.ChargedBy)
.HasForeignKey(t => t.ChargedById)
.HasPrincipalKey(m => m.Id);