fluent nhibernate - 具有属性的多对多映射 [英] fluent nhibernate - Many to Many mapping with attribute
问题描述
我有 2 个主表,它们由一个映射表链接,如下所示
I have 2 master tables which are linked by a map table as below
User [UserId,Name]
Resource [ResourceId,Name]
UserResourceMap [UserId,ResourceId,AccessLevel]
将 AccessLevel 作为资源属性的 Resource 和 User ClassMap 看起来如何?
How would the Resource and User ClassMap with AccessLevel as a resource attribute look?
My Domain 类看起来像这样
My Domain classes look like this
public class User
{
public virtual int UserId { get;protected set; }
public virtual string Name { get;set; }
}
public class Resource
{
public virtual int ResourceId { get;protected set; }
public virtual string Name { get;set; }
public virtual string AccessLevel { get;set; }//Issue-populate this using fluent
}
如何使用 fluent 映射以下代码中的 accessLevel 属性.
How can I use fluent to map the accessLevel attribute in the below code.
public class UserMap : ClassMap<User>
{
public UserMap()
{
Table("User");
Id(x => x.Key);
Map(x=>x.Name);
}
}
public class ResourceMap : ClassMap<Resource>
{
public ResourceMap()
{
Table("Resource");
Id(x => x.Key);
Map(x=>x.Name);//Need some Map Here to make a hasManyToMany Map with attribute
}
}
推荐答案
您的域模型似乎与您的数据库模型不匹配 - Resource 类具有属性 AccessLevel(即每个资源一个 AccessLevel)但在 DB 模型中 AccessLevel 是映射表上的一列(即每个用户-资源关系一个访问级别).
Your domain model does not seem to match your database model - the Resource class has the property AccessLevel (i.e. one AccessLevel per Resource) but in the DB model AccessLevel is a column on the map table (i.e. one AccessLevel per User-Resource relation).
假设 DB 模型是正确的模型,一种(相当简单的)映射方法就是引入这样的类.
Assuming the DB model is the correct model one (fairly straightforward) way of mapping this would be to introduce a class like this.
public class UserResource {
public virtual int UserResourceId { get; protected set; }
public virtual User User { get; set; }
public virtual Resource { get; set; }
public virtual string AccessLevel { get; set; }
}
并以这种方式映射:
public class UserResourceMap : ClassMap<UserResource> {
public UserResourceMap() {
Table("UserResourceMap");
Id(x => x.UserResourceId);
References(x => x.User).UniqueKey("UniqueUserAndResource");
References(x => x.Resource).UniqueKey("UniqueUserAndResource");
Map(x => x.AccessLevel);
}
}
如果您想要双向关联,您还可以在 User 和/或 Resource 上添加 Collection 属性,并使用 HasMany(...).Inverse() 映射它们.当然,这种映射会在 UserResourceMap 表中引入一个新的 UserResourceId 列(使用由 User 和 Resource 组成的复合键可以缓解这种情况).
If you want bidirectional associations you could also add a Collection property on User and/or Resource and map these with HasMany(...).Inverse(). Of course, this kind of mapping would introduce a new UserResourceId column in the UserResourceMap table (using a composite key consisting of User and Resource would mitigate that).
另一种解决方案是添加 EntityMap 关联.如果关联归用户所有,它将是 Dictionary
Another solution would be to add an EntityMap association. If the association is owned by User it would be a Dictionary<Resource, string> property. Something like this might do the trick:
public class User {
public virtual int UserId { get; protected set; }
public virtual string Name { get; set; }
public virtual Dictionary<Resource, string> Resources { get; set; } // Resource -> AccessLevel
}
public class UserMap : ClassMap<User> {
public UserMap() {
Table("User");
Id(x => x.UserId);
Map(x => x.Name);
HasMany<Resource, string>(x => x.Resources).AsEntityMap().Element("AccessLevel");
}
}
这篇关于fluent nhibernate - 具有属性的多对多映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!