首页 新闻 赞助 找找看

NHibernate 3.2 Conformist导航属性配置

0
悬赏园豆:10 [已解决问题] 解决于 2012-07-19 09:58

请问园子里面有用Conformist进行关联表配置的么? 一对一,一对多,多对多的情况,这一块网上说明很少,有点迷惑,谢谢。

随处走走的主页 随处走走 | 初学一级 | 园豆:123
提问于:2011-10-21 13:30
< >
分享
最佳答案
0
    class OneToOneDemo
{
[Test]
public void UnidirectionalOneToOneMappingDemo()
{
var mapper = new ModelMapper();
mapper.Class<Person>(cm =>
{
cm.Id(person => person.Id, map => map.Generator(Generators.HighLow));
cm.Property(person => person.Name);
cm.ManyToOne(person => person.Address);
});
mapper.Class<Address>(cm =>
{
cm.Id(address => address.Id, map => map.Generator(Generators.HighLow));
cm.Property(address => address.CivicNumber);
cm.Property(address => address.Street);
});

var hbmMapping = mapper.CompileMappingForAllExplicitlyAddedEntities();
hbmMapping.ShowInConsole();
}

[Test]
public void BidirectionalOneToOnePrimaryKeyAssociationMappingDemo()
{
var mapper = new ModelMapper();
mapper.Class<Person>(cm =>
{
cm.Id(person => person.Id, map => map.Generator(Generators.HighLow));
cm.Property(person => person.Name);
cm.OneToOne(person => person.Address, map => { });
});
mapper.Class<Address>(cm =>
{
cm.Id(address => address.Id, map => map.Generator(Generators.Foreign<Address>(address=>address.Person)));
cm.Property(address => address.CivicNumber);
cm.Property(address => address.Street);
cm.OneToOne(address=>address.Person,map=>map.Constrained(true));
});
var hbmMapping = mapper.CompileMappingForAllExplicitlyAddedEntities();
hbmMapping.ShowInConsole();
}

[Test]
public void BidirectionalOneToOneForeignKeyAssociationMappingDemo()
{
var mapper = new ModelMapper();
mapper.Class<Person>(cm =>
{
cm.Id(person => person.Id, map => map.Generator(Generators.HighLow));
cm.Property(person => person.Name);
cm.ManyToOne(person => person.Address,map=>map.Unique(true));
});
mapper.Class<Address>(cm =>
{
cm.Id(address => address.Id, map => map.Generator(Generators.HighLow));
cm.Property(address => address.CivicNumber);
cm.Property(address => address.Street);
cm.OneToOne(address => address.Person, map => map.PropertyReference(typeof(Person).GetProperty("Address")));
});

var hbmMapping = mapper.CompileMappingForAllExplicitlyAddedEntities();
hbmMapping.ShowInConsole();
}
}

ManyToManyDemo

class ManyToManyDemo
{
[Test]
public void UnidirectionalManyToManyMappingDemo()
{
//I will show how you can work with many-to-many
var mapper = new ModelMapper();
mapper.Class<Address>(cm =>
{
cm.Id(address => address.Id, map => map.Generator(Generators.GuidComb));
cm.Property(address => address.CivicNumber);
cm.Property(address => address.Street);
});
mapper.Class<Person>(cm =>
{
cm.Id(person => person.Id, map => map.Generator(Generators.GuidComb));
cm.Property(person => person.Name);
cm.Set(person => person.Addresses, map => { }, cer => cer.ManyToMany());
});
//Show the mapping to the console
var hbmMapping = mapper.CompileMappingForAllExplicitlyAddedEntities();
hbmMapping.ShowInConsole();
}

[Test]
public void BidirectionalManyToManyMappingDemo()
{
//I will show how you can work with many-to-many
var mapper = new ModelMapper();
mapper.Class<User>(cm =>
{
cm.Id(user => user.Id, map => map.Generator(Generators.GuidComb));
cm.Property(user => user.Name);
cm.Set(user => user.Roles,map=> { },cer=>cer.ManyToMany());
});
mapper.Class<Role>(cm =>
{
cm.Id(role => role.Id, m => m.Generator(Generators.GuidComb));
cm.Property(role => role.Name);
cm.Set(role => role.Users, map => { }, km => km.ManyToMany());
});
//Show the mapping to the console
var hbmMapping = mapper.CompileMappingForAllExplicitlyAddedEntities();
hbmMapping.ShowInConsole();
}
}

是不是很简单。

收获园豆:10
李永京 | 老鸟四级 |园豆:3114 | 2011-10-24 17:08

请问延时加载是怎么实现的呢? 我两个一对多,这样子做: 

Bag<UserRole>(entity => entity.UserRoles, x =>
{
x.Key(k => k.Column("UserID"));
x.Lazy(CollectionLazy.Lazy);
x.Fetch(CollectionFetchMode.Join);
},
y => y.OneToMany()
);


ManyToOne<UserInfo>(x => x.UserInfo, map =>
{
map.Class(typeof(UserInfo));
map.Column("UserID");
//map.Lazy(LazyRelation.Proxy);
map.Fetch(FetchKind.Join);
});

可是这样系统还是直接就加载了UserRole数据,UserRole又加载了UserInfo的数据,导致一个死循环了...



随处走走 | 园豆:123 (初学一级) | 2011-12-07 16:12
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册