简体   繁体   English

流畅的NHibernate映射

[英]Fluent NHibernate mapping

I'm new to NHibernate and Fluent NHibernate. 我是NHibernate和Fluent NHibernate的新手。

Assuming I have a situation like the following 假设我有以下情况

Table Activities (uniquidentier ID, varchar ActivityName)
Table ActivityParameters(uniqueidentifier ID, varchar ParameterName,
 varbinary(8000) ParameterValue)

and the following class 和下面的课程

public static Acivity
{
     .......
     public virtual Guid Id {get; private set;}      
     public virtual string ActivityName {get; private set;}
     public virtual IDictionary<string, object> ActivityParameters {get; private set;}
}

how can i write the classmap? 我该如何编写类图? More specifically, how can i write the mapping for activityparameters? 更具体地说,我如何编写活动参数​​的映射?

A colleague pointed e to this site . 一位同事指出了这个网站

Based on that discussion, I've come to 基于这个讨论,我来了

Table("Activities");
        Id(x => x.Id).Column("ID").GeneratedBy.Guid();
        Map(x => x.ActivityName).Not.Nullable().Length(50);
        HasMany(x => x.ActivityParameters)
            .KeyColumn("ActivityID")
            .AsMap<string>(idx => idx.Column("ParameterName"), elem => elem.Column("ParameterValue"))
            .Not.LazyLoad()
            .ForeignKeyCascadeOnDelete()
            .Table("ActivityParameters");

I have to test this. 我必须测试一下。

This seems helpful. 似乎有帮助。

Theoretically it should look like: 从理论上讲它应该是这样的:

public class ActivityClassMap : ClassMap<Activity>
{
    ...
    HasMany(x => x.ActivityParameters) 
      .AsMap(x=>x.NameOfParameterOrWhatever)
      .KeyColumnNames.Add("ParameterId"); 
    ...
}

But that's just a small investigation through google - I didn't try this in practice. 但这只是通过谷歌的一个小调查 - 我没有在实践中尝试这一点。

Ps don't forget to pick up the newest version. Ps不要忘记拿起最新版本。

Based on your hint, i've come to: 根据你的提示,我来:

WithTable("Activities"); 
Id(x => x.Id).ColumnName("ID").GeneratedBy.Guid(); 
Map(x => x.ActivityName).Not.Nullable().WithLengthOf(50); 
HasMany(x => x.ActivityParameters) 
        .Cascade.Delete() 
        .KeyColumnNames.Add("ActivityID") 
        .AsMap("ParameterName") 
        .AsMap("ParameterValue") 
        .WithTableName("ActivityParameters");

but i get an error, Association references unmapped class: System.Object. 但我得到一个错误,关联引用未映射的类:System.Object。 :( :(

Later edit: I got the latest version of Fluent, and now the code looks like this: 稍后编辑:我得到了最新版本的Fluent,现在代码如下:

Table("Activities");
        Id(x => x.Id).Column("ID").GeneratedBy.Guid();
        Map(x => x.ActivityName).Not.Nullable().Length(50);
        HasMany(x => x.ActivityParameters)
            .KeyColumn("ActivityID")
            .ForeignKeyCascadeOnDelete()
            .Table("ActivityParameters");

You actually need to define what kind of object your value property in IDictionary does contain - NHibernate doesn't know how to map . 实际上你需要定义IDictionary中的value属性包含哪种对象 - NHibernate不知道如何映射。 You should use a specific Class instead of . 您应该使用特定的类而不是。 Then you specify the Mapping for this class as well. 然后,您也可以为此类指定Mapping。

Please forgive my ignorance. 请原谅我的无知。

I worked with NHibernate about 6 months ago and still were we opting for the XML integration mapping, then compiling the XML mappings within the library or whatsoever. 大约6个月前我和NHibernate合作过,我们仍然选择XML集成映射,然后编译库中的XML映射或者任何东西。

Is this new to map relational tables and class objects directly from within the code? 这是新的直接从代码中映射关系表和类对象吗?

I heard this was coming somehow with the Entity Framework coming out by specifying the mapping just on top of each element of a class within square brackets. 我听说这是通过在方括号内的类的每个元素之上指定映射而实现的实体框架。 Thus, since Entity Framework was solely for use with SQL Server, we couldn't think of using it as we were working in a multi-DB environment with SQL Server, Oracle and SQLite. 因此,由于实体框架仅用于SQL Server,我们无法考虑使用它,因为我们在SQL Server,Oracle和SQLite的多数据库环境中工作。

Thanks for any lighting on the subject! 感谢关于这个主题的任何照明! :-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM