'How to configure one way One-to-One relationship in EF Core?
I have this
public class BankAccount
{
public Guid Id { get; set; }
public string BankName { get; set; }
public string AccountNo { get; set; }
public Guid CurrencyId { get; set; }
public Currency Currency { get; set; }
}
public class Currency
{
public Guid Id { get; set; }
public string Code { get; set; }
public string Name { get; set; }
}
Currency is used by many table, for example BankAccount and PurhasePayment. So I've done this in my EntityTypeBuilder<BankAccount> builder
builder.HasOne(c => c.Currency)
.WithOne()
.HasForeignKey<BankAccount>(e => e.CurrencyId);
However, when I checked the database. It's creating a unique
CREATE UNIQUE INDEX ix_bank_account_currency_id ON public.bank_account USING btree (currency_id);
which is causing duplicate error when more than one records using the same Currency for example USD.
What's the correct FluentAPI syntax? I'm using EF Core 6. Any help will be appreciated.
Solution 1:[1]
builder.HasOne(c => c.Currency)
.WithMany()
.HasForeignKey(e => e.CurrencyId);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | warheat1990 |
