c# - EF Core 支持字段字符串数组
问题描述
看到这个页面后,上面写着:
支持字段允许 EF 读取和/或写入字段而不是属性。...默认情况下,EF 将始终读取和写入支持字段 - 假设已正确配置 - 并且永远不会使用该属性。
我想我会试一试。该属性是 EF Core 不支持的字符串数组,所以我想我可以使用字符串作为支持字段。
private string ImagesString;
[BackingField(nameof(ImagesString))]
public string[] Images
{
get
{
return ImagesString.Split('\n');
}
set
{
ImagesString = string.Join('\n', value);
}
}
我的假设是 EF Core 不会尝试保存Images
,而只会尝试保存ImagesString
纯字符串。但是,当我尝试它时,我得到了相同的“Images
无法映射该属性,因为它是类型string[]
......”为什么要尝试保存Images
?它不应该只尝试保存/恢复ImagesString
吗?
解决方案
首先,您忘记放入DbContext UsePropertyAccessMode的OnModelCreating 方法(在您提供的链接中也提到过)。
但是,您会得到:
The specified field 'ImagesString' of type 'string' cannot be used for the property 'Class.Images' of type 'string[]'. Only backing fields of types that are compatible with the property type can be used.
因此,我认为 EF 现在无法处理此转换。
我建议您根据需要在实体中创建公共方法或 [NotMapped] 属性。
推荐阅读
- php - 在php中安装composer时遇到问题
- c# - 无法从单元测试程序集中访问静态变量
- reactjs - [material-ui][DatePicker] 为 DatePicker/TimePicker/DateTimePicker 更改 @material-ui/pickers Ok/Cancel 对话框按钮文本颜色
- javascript - 我如何跳过 document.querySelectorAll 的第一个结果
- java - 带有 com.sun.net.httpserver.HttpServer 的 Java SSE 服务器
- elasticsearch - 将 prem logstash 实例连接到 AWS 弹性云
- node.js - 从 AWS S3 请求错误的对象会破坏节点服务器
- javascript - 如何使用javascript在特定页面上重定向
- flutter - Flutter 变量(getter)在不同的类中调用时始终为空
- php - 如何仅根据字符串搜索特定文件名?