Fluent NHibernate mapping

2019-03-15 19:39发布

I'm new to NHibernate and 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?

5条回答
相关推荐>>
2楼-- · 2019-03-15 20:08

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.

P.s. don't forget to pick up the newest version.

查看更多
3楼-- · 2019-03-15 20:09

You actually need to define what kind of object your value property in IDictionary does contain - NHibernate doesn't know how to map . You should use a specific Class instead of . Then you specify the Mapping for this class as well.

查看更多
地球回转人心会变
4楼-- · 2019-03-15 20:09

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. :(

Later edit: I got the latest version of Fluent, and now the code looks like this:

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");
查看更多
Deceive 欺骗
5楼-- · 2019-03-15 20:11

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.

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.

Thanks for any lighting on the subject! :-)

查看更多
等我变得足够好
6楼-- · 2019-03-15 20:24

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.

查看更多
登录 后发表回答