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();
}
}
是不是很简单。
请问延时加载是怎么实现的呢? 我两个一对多,这样子做:
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的数据,导致一个死循环了...