Using the fluent api, how do I map a custom type as the primary key within my OnModelCreating method of the DbContext class?
Using EF Core I'm trying to build a model for the follow entity.
public class Account
{
public AccountId AccountId { get; }
public string Name { get; set; }
private Account()
{
}
public Account(AccountId accountId, string name)
{
AccountId = accountId;
Name = name;
}
}
Where the primary key is the AccountId
; the type is a simple value object like this.
public class AccountId
{
public string Id { get; }
public AccountId(string accountId)
{
Id = accountId;
}
}
Within OnModelCreating
, I found I can't map the AccountId
without having a backing field. So I introduced the backing field _accountId
. I don't want the AccountId to have a setter.
public class Account
{
private string _accountId;
public AccountId AccountId { get { return new AccountId(_accountId); } }
public string Name { get; set; }
private Account()
{
}
public Account(AccountId accountId, string name)
{
_accountId = accountId.Id;
Name = name;
}
}
But I still can't figure out how you specify a property with a backing field which is also the primary key.
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
var account = modelBuilder.Entity<Account>();
account.ToTable("Accounts");
account.HasKey(x => x.AccountId);
account.Property(x => x.AccountId).HasField("_accountId");
}
The OnModelCreating throws an exception on the property map line (account.Property(x => x.AccountId).HasField("_accountId");
).
Stating that property and field have to be the same type.