实体框架代码第一列表< string>物业映射 [英] Entity Framework Code First List<string> Property Mapping

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

问题描述

我正在使用实体框架代码。我有一个简单的模型:

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

public int Id {get;组; }

public IList< string> TextOptions
{
get;
设置;
}
}

我碰到了Property TextOptions 的类型为列表< String>



当我尝试在Entity Framework中执行此操作,它不会映射。



我在这里找到一个解决方案(stackoverflow)来解决我的问题。我基本上修改了我的课程,以便它使用列表,并使其成为一个被分隔的字符串,而不是被持久化:

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

public int Id {get;组; }

public virtual IList< string> TextOptions
{
get
{

return _TextOptions;

}
set
{
_TextOptions = value;
}
}

私人IList< string> _TextOptions;

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

此代码工作正常。我所关心的问题是我认为这违反了分离关注。我不认为我的模型类应该关注序列化字符串列表,以便Entity框架可以持久化。



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



有没有办法我可以为Entity Framework清洁为自定义模型绑定器用于MVC?

解决方案

不,EF没有任何类型转换器或自定义类型映射器作为替代从MVC建模粘合剂。您必须始终使用一些黑客来强制持久化。另一种做法是将 TextOptions 映射为相关实体的集合。这将使您更好地分解问题,但会使您的模型复杂化,并使用变量

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

public int Id {get;组; }

public IList< TextOption> TextOptions
{
get;
设置;
}
}

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


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;
    }
}

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.

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.

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.

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

解决方案

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; }
}

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

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