映射一个List<串GT;用流利的NHibernate分隔字符串 [英] Mapping a List<string> to a delimited string with Fluent NHibernate

查看:186
本文介绍了映射一个List<串GT;用流利的NHibernate分隔字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模型看起来是这样的:

My model looks something like this:

public class Product
{
    public string Name {get; set;}
    public string Description {get; set;}
    public double Price {get; set;}
    public List<string> Features {get; set;}
}



我希望我的数据库表是平的 - 这样的名单应该是存储为一个分隔的字符串:
有一张|具有两个|例如突出三种

I want my database table to be flat - the List should be stored as a delimited string: Feature one|Feature two|Feature three for example.

在从数据库中检索到,应该把其中的每一项后面到一个List

When retrieved from the db, it should place each of those items back into a List

这可能吗?

推荐答案

我做同样的在我的当前项目,只有我坚持枚举作为管道分隔数的集合。它的工作方式相同

I'm doing the very same in my current project, only I'm persisting a collection of enums as pipe-delimited numbers. It works the same way.

public class Product
{
    protected string _features; //this is where we'll store the pipe-delimited string
    public List<string> Features {
        get
        {
            if(string.IsNullOrEmpty(_features)
                return new List<String>();
            return _features.Split(new[]{"|"}, StringSplitOptions.None).ToList();
        }
        set
        {
            _features = string.Join("|",value);
        }
    }
}

public class ProductMapping : ClassMap<Product>
{
    protected ProductMapping()
    {
        Map(x => x.Features).CustomType(typeof(string)).Access.CamelCaseField(Prefix.Underscore);
    }
}

这篇关于映射一个List&LT;串GT;用流利的NHibernate分隔字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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