Abs*_*lom 7 entity-framework .net-core
问题: Entity Framework Core 3.0 中是否有一种方法可以在支持字段上应用值转换?
上下文: 假设我有一个博客实体,其中包含表示帖子 ID 的字符串值列表。我想避免需要连接实体/表(如此处所述),因此我进行转换以将列表作为字符串存储在数据库中。我还想保护我的模型不被直接通过 PostIds 属性修改,因此我想为此使用支持字段(如此处所述)。
像这样的东西:
public class Blog
{
public int BlogId { get; set; }
private readonly List<string> _postIds;
public IReadOnlyCollection<string> PostIds => _postIds;
public Blog()
{
_posts = new List<Post>();
}
}
Run Code Online (Sandbox Code Playgroud)
上下文的配置看起来像这样:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Configuring the backing field
modelBuilder.Entity<Blog>()
.Metadata
.FindNavigation(nameof(Blog.PostIds))
.SetPropertyAccessMode(PropertyAccessMode.Field);
// Trying to configure the value conversion, but that doesn't work...
modelBuilder.Entity<Blog>()
.Property(e => e.PostIds)
.HasConversion(v => string.Join(',', v),
v => v.Split(','));
}
Run Code Online (Sandbox Code Playgroud)
您知道如何使用当前版本的实体框架(3.0)来实现此配置吗?
| 归档时间: |
|
| 查看次数: |
563 次 |
| 最近记录: |