Reputation: 4439
private int _billAccount;
public string BillAccount { get
{
var account = _billAccount.ToString().PadLeft(9, '0');
return account.Substring(0, 3) + "-" + account.Substring(3, 3) + "-" + account.Substring(6, 3);
}
set
{
var account = new string(value.Where(char.IsDigit).ToArray());
_billAccount = Convert.ToInt32(account);
}
}
The BillAccount property in the class handles the string presentation as (999-999-999). The column in the table is defined as
BillAccount INT NOT NULL
I've tried using
.HasField("_billAccount")
.HasConversion(new NumberToStringConverter<int>())
and any other combination I can think of, but I just can't get it to work.
System.InvalidOperationException: The specified field '_billAccount' of type 'int' cannot be used for the property 'Lead.BillAccount' of type 'string'. Only backing fields of types that are assignable from the property type can be used
How can I configure this in EF Core 3, Fluent API so field _billAccount
is mapped to table column BillAccount
, even though they have a different types?
Upvotes: 0
Views: 862
Reputation: 33738
as @madreflection alluded to:
public class TheModel {
private int _billAccount;
private string _formattedBillAccount = string.Empty;
public int BillAccount {
get { return _billAccount; }
set {
_billAccount = value;
_formattedBillAccount = value.ToString().PadLeft(9, '0');
_formattedBillAccount = $"{_formattedBillAccount.Substring(0, 3)}-{_formattedBillAccount.Substring(3, 3)}-{_formattedBillAccount.Substring(6, 3)}";
}
}
[NotMapped]
public string FormattedBillAccount { get { return _formattedBillAccount; } }
}
Upvotes: 1