mapping collection type with Fluent NHibernate
I've used Fluent NH in my project but I'm having some problems with using the Collection class. Her开发者_Go百科e's the code for my classes
public class Vendor
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual Services Services { get; set; }
}
public class Services : IList<Service>
{
}
public class Service
{
int id{ get; set; }
int Code { get; set; }
}
this instead of put service as list in the vendor class
public virtual IList<Service> Services { get; set; }
I want to use services collection class.
and the mapping code
public class VendorMap : ClassMap<Vendor>
{
public VendorMap()
{
Table("Vendor");
Id(x => x.Id);
Map(x => x.Name);
HasMany<Service>(x => x.Services)
.KeyColumn("Vendor_Id")
.CollectionType<Services>()
.Not.LazyLoad();
}
I got this error "Custom type does not implement UserCollectionType: Services"
Any ideas on how to map this?
Thanks in advance.
Try this :
HasMany(x => x.Services)
.KeyColumn("Vendor_Id")
.AsBag()
.Cascade.All()
.Not.LazyLoad();
It works great for me!
NHibernate does not permit mapping collection classes of this type. They must be an interface, like IList<T>
, as NHibernate provides it's own implementation.
This implementation obviously does not meet the interface of the Services
class, so NHibernate is unable to map it.
精彩评论