建立一个与功能NHibernate一对多的关系 [英] Setting up one to many relationship with Fluent Nhibernate

查看:122
本文介绍了建立一个与功能NHibernate一对多的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一点与我的同事的说法,我似乎无法找到一个答案,然而,这是很基本的东西。

I'm having a bit of an argument with my co-worker that I can't seem to find an answer to yet this is very basic stuff.

建立一到许多流利NHibernate的实体关系。

Establishing one-to-many relationship in Fluent Nhibernate entity.

让我们以角色和用户为例。角色可以分配给多个用户,所以我做了我的实体胆量是这样的:

Let's take Roles and users for example. A role can be assigned to multiple users so I made my entity guts looks like:

public class User
{
    [Required]
    public virtual string FirstName { get; set; }
    public virtual Role Role { get; set; }
}

和作用

public class Role
{
    [Required]
    public virtual string Name { get; set; }
    public virtual IList<User> Users{ get; set; }

    public Role()
    {
        Users = new List<Users>();
    }
}



正如你可以看到,我引用的集合在角色的用户,一种说,每个角色都会有多个用户。用户实体必须确定并用户属于哪个角色需要的角色实体引用。

As you can see I'm referencing a collection of Users in roles, kind of saying that each role will have multiple users. User entity has that Role entity reference needed to identify what Role does a user belong to.

在我看来这是链接正确的方式和我的同事说,具有用户角色引用将创建一个循环引用。谁是对的?

In my opinion this is the correct way to link and my co-worker says that having a Role reference for users will create a circular reference. Who's right ?

我试图寻找答案在线。我认为这个问题告诉我,我是正确的:
功能NHibernate许多人一对一映射

I tried finding the answer online. I think this question tells me that I'm right: Fluent NHibernate Many to one mapping

但后来我看了Fuent NHibernate的示例项目在这里
https://github.com/jagregory/fluent-nhibernate/tree/master/src/Examples.FirstAutomappedProject/Entities 和我不'T的什么,我想实现一个例子。
你们可以建议或帮助我找到一个文件,解释正确的方法是什么?我对吗?
谢谢你。

But then I looked at a Fuent Nhibernate sample project here https://github.com/jagregory/fluent-nhibernate/tree/master/src/Examples.FirstAutomappedProject/Entities and I don't an example of what I'm trying to implement. Can you guys advise or help me find a document explaining the correct way ? Am I right? Thank you.

推荐答案

你所建议这里是完全可能的,并允许NHibernate的框架之内。很明显,你已经列出的型号,而不是映射文件,但功能NHibernate允许你以这种方式没有问题,配置您的映射。

What you are proposing here is entirely possible and allowable within the nHibernate framework. Obviously you have listed the models rather than the mapping files but Fluent nHibernate allows for you to configure your mappings in this way without issue.

无论你其实选择映射关系以这种方式完全是个人喜好和具体情形。我制订这样的模型,但同样都选择了不,因为在某些情况下,主要是它没有意义的对象图过于复杂化。举个例子,引用跨多个表多次在数据库中的查询表(如文化或语言环境),映射这个时候我会在父模型中的每一个文化属性,但不会有父对象的集合在文化模型 - 它只是没有任何意义。

Whether you actually choose to map a relationship in this way is entirely down to personal preference and the specific scenario. I have mapped models in this way but equally have chosen not to, mainly because in some instances it doesn't make sense to overly complicate the object graph. Take for example a look-up table (e.g. Culture or Locale) that is referenced many times across multiple tables in a database, when mapping this I would have a Culture property in each one of the parent models but would not have collections of parent objects in the Culture model - it just doesn't make sense.

您还需要通过持久层的考虑加载数据 - 如果你创建了这种关系,只需要你需要考虑的时候,或者角色的一个简单的列表,用户集合填充 - 你可以指定渴望或延迟加载,但在我的经验中指定预先加载在该查询相应的取指令可能会导致更优化的调用数据库。

You also need to consider loading data through your persistence layer - if you create this sort of relationship and just need a simple list of roles you need to consider when, or if , the users collection is populated - you can specify eager or late loading but in my experience specifying eager loading with a corresponding Fetch command in the Queries can result in a more optimized call to the database.

基本上我要说的是,没有绝对的正确的方式决定如何定义你的映射时 - 你真的需要平衡一个丰富的对象模型与查询性能,但是你的具体的例子是,如果你需要它完全可以接受的。

Basically what I am saying is that there is no absolute "correct way" when deciding how to define your mappings - you really need to balance a rich object model vs. query performance, but your specific example is perfectly acceptable if you need it.

我刚刚重新阅读你的问题,我不知道你是否也要求的例子映射以这种方式配置,所以如果你想要一些例子让我知道,我会放一些一起给你。

I have just re-read your question and I am not sure if you are also asking for examples of mappings configured in this way so if you do want some examples let me know and I'll put some together for you.

要实现你的描述,你将需要之间的关系下面的地图类(我已经添加标识属性,我猜你有这些):

To achieve the relationship you describe you would need the following map classes (I have added Id properties as I guess you have those):

有关的角色:

public class RoleMap : ClassMap<Role>
{
    public RoleMap()
    {
          Table(@"Roles");
          Id(x => x.Id).GeneratedBy.Assigned();
          Map(x => x.Name).Column("Name");
          HasMany<User>(x => x.Users)
            .Inverse()
            .KeyColumns.Add("RoleId", mapping => mapping.Name("RoleId"));
    }
}

有关的用户:

public class UserMap : ClassMap<User>
{
   public UserMap()
    {
          Table(@"Users");
          Id(x => x.Id).GeneratedBy.Assigned();
          Map(x => x.FirstName);
          Map(x => x.RoleId);    
          References(x => x.Role)
            .Class<Role>()
            .Columns("RoleId");
    }
}



我也不会太挂在您的法VS他们的方法 - 上面是完全可以接受的,而取决于您的要求,当你来到使用对象模型代码。另外,如果你不想在角色映射一个集合的用户只需删除该属性和相关的hasMany映射声明。有一件事从上面需要注意的是,.Inverse()规范委派一个角色和一个用户到用户的实体之间的关系,这是有道理的基本上添加或编辑现有用户,并提供了一个角色ID的过程管理将建立的关系。

I wouldn't get too hung up on "your method vs their method" - the above is perfectly acceptable, and depends on your requirements when you come to use the object model in your code. Alternatively if you don't want a Users collection in the Role Map just remove that property and the associated HasMany mapping declaration. One thing to note from the above is that the .Inverse() specification has delegated management of the relationship between a Role and a User to the Users entity, which makes sense as basically the process of adding or editing an existing user and providing a RoleId will forge the relationship.

希望这有助于有点如果您有任何更具体的问题,让我知道

Hope this helps somewhat if you have any more specific questions let me know

这篇关于建立一个与功能NHibernate一对多的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆