开发者

NHIBERNATE: How to map a collection into the DB using Mapping.Attributes

I Have this Product Class i took from the examples of how to use nhibernate (but i did some modifications to it) I want to be able to map a class and all of it's references into the DB.

[NHibernate.Mapping.Attributes.Class(Lazy=true)]
    public class Product
    {             
        [NHibernate.Mapping.Attributes.Id(Name="Id",TypeType=typeof(Guid))]
        public virtual Guid Id { get; set; }

        [NHibernate.Mapping.Attributes.Property]
        public virtual string Name { get; set; }

        [NHibernate.Mapping.Attributes.Property]
        public virtual int Category { get; set; }

        [NHibernate.Mapping.Attributes.Property]
       开发者_开发知识库 public virtual bool Discontinued { get; set; }

        [NHibernate.Mapping.Attributes.Map(Name="Items")]
        public virtual Dictionary<Guid, SubItem> Products { get; set; }
    }

where subitem is defined as follows:

  [NHibernate.Mapping.Attributes.Class(Lazy = true)]
    public class SubItem
    {
        [NHibernate.Mapping.Attributes.Id(Name = "Id", TypeType = typeof(Guid))]
        public virtual Guid Id { get; set; }

        [NHibernate.Mapping.Attributes.Property]
        public virtual string Name { get; set; }              
    }

and i get the following exeption yielding that "The element 'map' in namespace 'urn:nhibernate-mapping-2.2' has incomplete content". probably something it missing overthere but i don't know what. and if it is the right way to do it. ( i want to be able to serialize the following object:

SubItem sub1 = new SubItem();
sub1.Id = Guid.NewGuid();
sub1.Name = "sub1";
SubItem sub2 = new SubItem();
sub2. Id  = Guid.NewGuid();
sub2.Name = "sub2";

Product p= new Product();
p.Name = "itay";
p.Category = 111;
p.Discontinued = false;
p.Id = Guid.NewGuid();

Product p1= new Product();
p1.Name = "Joe";
p1.Category = 222;
p1.Discontinued = true;
p1.Id = Guid.NewGuid();

p.Products = new System.Collections.Generic.Dictionary<Guid, SubItem>();
p.Products.Add(sub1.Id, sub1);
p.Products.Add(sub2.Id, sub2);
p1.Products = new System.Collections.Generic.Dictionary<Guid, SubItem>();
p1.Products.Add(sub1.Id,sub1);

i wonder how will NHIBERNATE present this situation in the DB ( will he create a diffrent table for SubItem objects? how will he treat the Products property with the Map Attribute on it....

Edit: i want to be able to create a ManyToMany mapping - which in the DB will become a different table with the relevant Id's (ProductID,SubItemId)

Help will be appreciated!


You need to provide the Key and Index information for the Map. Your attributes would look something like...

    [Map(0, Inverse = true)]
    [Key(1, Column = "ProductId")]
    [Index(2, Column = "SubItemId", TypeType = typeof(Guid))]
    [OneToMany(3, ClassType = typeof(SubItem))]
    public virtual Dictionary<Guid, SubItem> Products { get; set; }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜