-
Notifications
You must be signed in to change notification settings - Fork 33
Entity Framework
Martijn Bodeman edited this page Dec 21, 2023
·
11 revisions
The Iban
type can be used in your domain and easily persisted in a varchar(34)
field with Entity Framework using a value converter.
The maximum length of any IBAN in electronic format is 34 characters. The actual length varies by country.
Your entity configuration on a typical SQL database would look something like this:
public record Account(Iban BankAccountNumber);
public class MyDbContext : DbContext
{
private IIbanParser _ibanParser;
public MyDbContext(IIbanParser ibanParser)
{
_ibanParser = ibanParser;
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var accountType = modelBuilder.Entity<Account>();
accountType
.Property(e => e.BankAccountNumber)
.HasColumnType("varchar(34)")
.HasConversion(
iban => iban.ToString(IbanFormat.Electronic),
ibanStr => _ibanParser.Parse(ibanStr)
);
}
}
For more information: https://learn.microsoft.com/en-us/ef/core/modeling/value-conversions