实体框架代码第一列表<string>属性映射 [英] Entity Framework Code First List&lt;string&gt; Property Mapping

查看:22
本文介绍了实体框架代码第一列表<string>属性映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我首先使用实体​​框架代码.我有一个简单的模型:

I am working with Entity Framework Code first. I have a simple model:

public class Variable
{
    public string Name { get; set; }

    public int Id { get; set; }

    public IList<string> TextOptions
    {
        get;
        set;
    }
}

我遇到了类型为 List 的属性 TextOptions 的问题.

I ran into issues with the Property TextOptions being of type List<String>.

当我尝试在实体框架中执行此操作时,它不会映射.

When I try to do this in Entity Framework, it does not map.

我在这里 (stackoverflow) 上找到了解决我的问题的解决方案.我基本上重新设计了我的课程,以便它获取列表并使其成为一个分隔的字符串,该字符串被持久化:

I found a solution on here (stackoverflow) that fixes my issue. I basically rework my class so that it takes the list and makes it a delimited string which gets persisted instead:

public class Variable : IVariable
{
    public string Name { get; set; }

    public int Id { get; set; }

    public virtual IList<string> TextOptions
    {
        get
        {

            return _TextOptions;

        }
        set
        {
            _TextOptions = value;
        }
    }

    private IList<string> _TextOptions;

    public string TextOptionsSerialized
    {
        get
        {
            return String.Join(";", _TextOptions);
        }
        set
        {
            _TextOptions = value.Split(new char[]{';'}, StringSplitOptions.RemoveEmptyEntries).ToList();
        }
    }
}

这段代码工作正常.我的问题是我认为它违反了关注点分离.我不认为我的模型类应该关注序列化字符串列表以便实体框架可以持久化它.

This code works fine. The problem that I have with it is that I think that it violates Separation of Concern. I don't think my model class should be concerned with serializing a list of strings so that Entity framework can persist it.

我在 ASP.Net MVC 中遇到了类似的问题.我从客户端发送了一个帖子,该帖子将映射到模型.与帖子相比,模型的结构方式存在一些问题.在 MVC 中,我可以编写一个自定义模型绑定器,以非常安全和可重用的方式处理转换.

I ran into a similar issue working in ASP.Net MVC. I had a post sent from the client that would be mapped to a model. There were some issues with the way the model was structured compared to the post. In MVC I could write a Custom Model Binder to handle the conversion in a very safe and reusable fashion.

对于实体框架,我有什么方法可以做到这一点,就像自定义模型绑定器对于 MVC 一样干净?

Is there ANY way I can do this for Entity Framework that is as clean as Custom Model Binders are for MVC?

推荐答案

不,EF 没有任何类型转换器或自定义类型映射器来替代来自 MVC 的模型绑定器.你必须总是使用一些 hack 来强制持久化.另一种方法是将 TextOptions 映射为相关实体的集合.它会使您更好地分离关注点,但会使您的模型和使用 Variable 的模型复杂化.

No, EF doesn't have any type converters or custom type mappers as alternative to model binders from MVC. You must always use some hack to force persistence. Another way to do the same is mapping TextOptions as collection of related entities. It will make your separation of concerns better but it will complicate your model and working with Variable.

public class Variable
{
    public string Name { get; set; }

    public int Id { get; set; }

    public IList<TextOption> TextOptions
    {
        get;
        set;
    }
}

public class TextOption
{
    public int Id { get; set; }
    public string Text { get; set; }
}

这篇关于实体框架代码第一列表<string>属性映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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