Chris McKee
Chris McKee

Reputation: 4387

How do I store a Serializable List<int> in FluentNhibernate

If I have the following (cut down for brevity)

public class TempCartMap : ClassMap<TempCart>
{
   Id(x => x.Id).GeneratedBy.Identity();
   Map(x => x.Products); // < This one
}

[Serializable]
public class TempCart {

  public TempCart(){
    Products = new List<int>();
  }

  public virtual IList<int> Products { get; set; }

}

I've looked at (http://www.philliphaydon.com/2012/06/using-nhibernate-with-servicestack/) and (http://www.philliphaydon.com/2012/03/ormlite-blobbing-done-with-nhibernate-and-serialized-json/) but is there a shorter, simpler, faster way of getting NHibernate to Serialize & De-serialize a column as above. Seems overkill to create a new IUserType class etc etc.

Upvotes: 1

Views: 287

Answers (1)

Jeroen
Jeroen

Reputation: 989

You can also store the Products-list as a separate table:

public class TempCartMap : ClassMap<TempCart>
{
    Id(x => x.Id).GeneratedBy.Identity();

    HasMany(x => x.Products)
        .Element("product_number")
        .KeyColumn("temp_cart_id")
        .Table("temp_cart_products")
    ;
}

Just to clarify the question: is there a reason why this is not wanted?

Upvotes: 1

Related Questions