Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fluent NHibernate mapping

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?

like image 954
DaeMoohn Avatar asked Aug 26 '09 16:08

DaeMoohn


People also ask

What is the difference between NHibernate and fluent NHibernate?

Fluent NHibernate offers an alternative to NHibernate's standard XML mapping files. Rather than writing XML documents, you write mappings in strongly typed C# code. This allows for easy refactoring, improved readability and more concise code.

Is NHibernate better than Entity Framework?

Entity framework requires additional connections to handle databases other than MSSQL, which nHibernate provides. In terms of data loading, SQL generation, custom column types, custom collections, and so on, NHibernate can be modified. However, the entity framework has limited extension points.


1 Answers

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.

like image 141
DaeMoohn Avatar answered Oct 15 '22 18:10

DaeMoohn