如何使用Entity Framework Core保留字符串列表? [英] How to persist a list of strings with Entity Framework Core?

查看:98
本文介绍了如何使用Entity Framework Core保留字符串列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们假设我们有一个类似于以下内容的类:

Let us suppose that we have one class which looks like the following:

public class Entity
{
    public IList<string> SomeListOfValues { get; set; }

    // Other code
}

现在,假设我们要使用EF Core Code First来保持这种状态,并且我们正在使用像SQL Server这样的RDMBS.

Now, suppose we want to persist this using EF Core Code First and that we are using a RDMBS like SQL Server.

显然,一种可能的方法是创建一个包装字符串的包装器类Wraper:

One possible approach is obviously to create a wraper class Wraper which wraps the string:

public class Wraper
{
    public int Id { get; set; }

    public string Value { get; set; }
}

并重构该类,使其现在依赖于Wraper对象的列表.在那种情况下,EF将为Entity生成一个表,为Wraper生成一个表,并建立一对多"关系:每个实体都有一堆包装器.

And to refactor the class so that it now depends on a list of Wraper objects. In that case EF would generate a table for Entity, a table for Wraper and stablish a "one-to-many" relation: for each entity there is a bunch of wrapers.

尽管这可行,但我不太喜欢这种方法,因为由于持久性问题,我们正在更改一个非常简单的模型.确实,仅考虑域模型和代码而没有持久性,Wraper类在那里就毫无意义了.

Although this works, I don't quite like the approach because we are changing a very simple model because of persistence concerns. Indeed, thinking just about the domain model, and the code, without the persistence, the Wraper class is quite meaningless there.

除了创建包装器类之外,是否还有其他方法可以使用EF Core Code First将具有字符串列表的一个实体持久保存到RDBMS?当然,最后必须做同样的事情:必须创建另一个表来保存字符串,并且必须存在一对多"关系.我只想使用EF Core做到这一点,而无需在域模型中编写包装类.

Is there any other way persist one entity with a list of strings to a RDBMS using EF Core Code First other than creating a wraper class? Of course, in the end the same thing must be done: another table must be created to hold the strings and a "one-to-many" relationship must be in place. I just want to do this with EF Core without needing to code the wraper class in the domain model.

推荐答案

从Entity Framework Core 2.1 开始,可以通过一种更加简单的方法来实现. EF现在支持值转换以专门解决诸如以下情况需要将属性映射到其他类型进行存储的地方.

This can be achieved in a much more simple way starting with Entity Framework Core 2.1. EF now supports Value Conversions to specifically address scenarios like this where a property needs to be mapped to a different type for storage.

要保留字符串集合,可以通过以下方式设置DbContext:

To persist a collection of strings, you could setup your DbContext in the following way:

protected override void OnModelCreating(ModelBuilder builder)
{
    var splitStringConverter = new ValueConverter<IEnumerable<string>, string>(v => string.Join(";", v), v => v.Split(new[] { ';' }));
    builder.Entity<Entity>().Property(nameof(Entity.SomeListOfValues)).HasConversion(splitStringConverter);
} 

请注意,此解决方案不会使您的业务类遇到数据库方面的问题.

Note that this solution does not litter your business class with DB concerns.

不用说这种解决方案,就必须确保字符串不能包含定界符.但是,当然,可以使用任何自定义逻辑进行转换(例如,从JSON到JSON的转换).

Needless to say that this solution, one would have to make sure that the strings cannot contains the delimiter. But of course, any custom logic could be used to make the conversion (e.g. conversion from/to JSON).

另一个有趣的事实是,将空值传递给转换例程,而是由框架本身处理.因此,无需担心转换例程中的空检查.但是,如果数据库包含NULL值,则整个属性将变为null.

Another interesting fact is that null values are not passed into the conversion routine but rather handled by the framework itself. So one does not need to worry about null checks inside the conversion routine. However, the whole property becomes null if the database contains a NULL value.

这篇关于如何使用Entity Framework Core保留字符串列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆