如何用Nhibernate(和Fluent NHibernate)映射Type [英] How to map Type with Nhibernate (and Fluent NHibernate)

查看:161
本文介绍了如何用Nhibernate(和Fluent NHibernate)映射Type的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public class DataType 
{
//对于NHibernate
private DataType(){}
$ b $ public DataType(string name,Type type,string defaultValue)
{
Name = name;
TypeOfContent = type;
DefaultInvariantStringValue = defaultValue;
}

public string Name {get;组; }
public Type TypeOfContent {get;组; }
public string DefaultInvariantStringValue {get;组; }



$ b $ p $如何映射属性TypeOfContent与NHibernate(流畅的映射将不胜感激)我会约束类型 nofollow noreferrer> C#内置类型例如字符串,整型,日期时间等,所以我想在数据库中存储System.String(对于一个字符串)

解决方案

我很好奇,为什么不这样做而不是

  public class DataType 
{
。 ..
私人字符串_typeOfContent;
public virtual Type TypeOfContent
{
get {return Type.GetType(_typeOfContent); }
set {_typeOfContent = value.FullName; }
}
}

...

public class DataTypeMap:ClassMap< DataType>
{
Map(x => x.TypeOfContent)
.Access.CamelCaseField(Prefix.Underscore)
.CustomType< string>();
}


Let's say I have something similar to this :

public class DataType
{
    //For NHibernate
    private DataType(){}

    public DataType(string name, Type type, string defaultValue)
    {
        Name = name;
        TypeOfContent = type;
        DefaultInvariantStringValue = defaultValue;
    }

    public string Name { get; set; }
    public Type TypeOfContent { get; set; }
    public string DefaultInvariantStringValue { get; set; }
}

How do I map the property TypeOfContent with NHibernate (fluent mapping would be appreciated too)?

I would constrain Type to C# Built-In Types e.g. string, int, datetime, etc. So I would like to store System.String (for a string) in the database

解决方案

I'm curious, why don't you do this instead

public class DataType
{       
    ...        
    private string _typeOfContent;
    public virtual Type TypeOfContent
    {
        get { return Type.GetType(_typeOfContent); }
        set { _typeOfContent = value.FullName; }
    }   
}

...

public class DataTypeMap : ClassMap<DataType>
{
    Map(x => x.TypeOfContent)
       .Access.CamelCaseField(Prefix.Underscore)
       .CustomType<string>();
}

这篇关于如何用Nhibernate(和Fluent NHibernate)映射Type的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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