【发布时间】:2019-10-16 17:50:24
【问题描述】:
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);
}
}
类中的 BillAccount 属性将字符串表示形式处理为 (999-999-999)。表中的列定义为
BillAccount INT NOT NULL
我尝试过使用
.HasField("_billAccount")
.HasConversion(new NumberToStringConverter<int>())
以及我能想到的任何其他组合,但我无法让它发挥作用。
System.InvalidOperationException:指定字段“_billAccount” 'int' 类型的不能用于属性 'Lead.BillAccount' 输入“字符串”。仅支持可分配类型的字段 属性类型可以使用
如何在 EF Core 3 中进行配置,Fluent API 使字段 _billAccount 映射到表列 BillAccount,即使它们具有不同的类型?
【问题讨论】:
-
将
BillAccount保留为int并使用未映射的属性进行翻译。
标签: c# entity-framework-core fluent