组件的概念比较简单,用途就是当作公共的组件来用,只定义一次,可以重复使用,
比如 类:Person 和 company,product 等等都有名字。人的名字有可以细分为firstname,lastname,fullname。
定义好Name以后这个name就可以让其他类似的类来复用
1.定义组件实体类Name
public class Name
{
public virtual string FirstName { set;get;}
public virtual string LastName { set;get;}
public virtual string FullName { set; get; }
}
2.映射组件类
public class NameMap: ComponentMap<Name>
{
public NameMap()
{
Map(x => x.FirstName);
Map(x => x.LastName);
Map(x => x.FullName);
}
}
3.修改原来的Person类,用组件来代替原来person类中firstname,lastname,fullname
public class PersonMap:ClassMap<Person>
{
public PersonMap()
{
Table("Person");
Id(x => x.ID);
Component(x => x.Name);
//Map(x => x.Name);
Map(x => x.Sex);
Map(x => x.Address);
}
}
4.最后调用,添加数据记录
Person p = new Person();
p.Name = new Name() { FirstName = "a", LastName = "b", FullName = "ab" };
p.Sex = "xxxxx";
p.Address = "aaaaaaaaaa";
using (var session = SessionFactory.CreateSession())
{
session.Save(p);
session.Flush();
}
比如 类:Person 和 company,product 等等都有名字。人的名字有可以细分为firstname,lastname,fullname。
定义好Name以后这个name就可以让其他类似的类来复用
1.定义组件实体类Name
public class Name
{
public virtual string FirstName { set;get;}
public virtual string LastName { set;get;}
public virtual string FullName { set; get; }
}
2.映射组件类
public class NameMap: ComponentMap<Name>
{
public NameMap()
{
Map(x => x.FirstName);
Map(x => x.LastName);
Map(x => x.FullName);
}
}
3.修改原来的Person类,用组件来代替原来person类中firstname,lastname,fullname
public class PersonMap:ClassMap<Person>
{
public PersonMap()
{
Table("Person");
Id(x => x.ID);
Component(x => x.Name);
//Map(x => x.Name);
Map(x => x.Sex);
Map(x => x.Address);
}
}
4.最后调用,添加数据记录
Person p = new Person();
p.Name = new Name() { FirstName = "a", LastName = "b", FullName = "ab" };
p.Sex = "xxxxx";
p.Address = "aaaaaaaaaa";
using (var session = SessionFactory.CreateSession())
{
session.Save(p);
session.Flush();
}
本文使用Blog_Backup未注册版本导出,请到soft.pt42.com注册。