nhibernate中的一对一映射 [英] one to one mapping in nhibernate

查看:100
本文介绍了nhibernate中的一对一映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用nhibernate.我的代码如下

I am using nhibernate. My code is as below

public class Store
    {
        public virtual int Id { get; protected set; }
        public virtual string Name { get; set; }
        public virtual IList<Employee> Staff { get; set; }

        public Store()
        {
            Staff = new List<Employee>();
        }
    }

员工阶层

    public class Employee
        {
            public virtual int Id { get; protected set; }
            public virtual string FirstName { get; set; }
            public virtual string LastName { get; set; }
            public virtual Store Store { get; set; }
        }

商店地图

    public class StoreMap:ClassMap<Store>
        {
            public StoreMap() 
            {
                Id(x => x.Id);
                Map(x => x.Name);
                HasMany(x => x.Staff).WithKeyColumn("store");
              }
        }

员工地图

public EmployeeMap ()
        {
            Id(x => x.Id);
            Map(x => x.FirstName);
            Map(x => x.LastName);
            References(x => x.Store  ).WithColumns("store");
        }

这很好.但是我想在员工和商店之间建立一对一的关系,为此,我尝试这样做,但是这不起作用. 在employeemap中 引用(x => x.Store).WithForeignKey("store"); 并在暴风雨中

this is working fine. but i want one to one relation between employee and store and for that i try this but this doesn't work. in employeemap References(x => x.Store).WithForeignKey("store"); and in stormap

HasOne(x => x.Staff).WithForeignKey("store");

但这会导致以下错误

未知的持久类:

 System.Collections.Generic.IList`1[[test.Models.Employee, test,
 Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]

请帮助

推荐答案

one-to-one映射非常具体.它说,一个对象不能存在而另一个则不能存在.检查文档 5.1.11.一对一,这对于流利的如何做到流利的休眠一对一映射?.

The one-to-one mapping is very specific. It says, that one object cannat exist without the other. Check docs 5.1.11. one-to-one and this for fluent How to do a fluent nhibernate one to one mapping?.

我们可以举个例子:

public StoreMap()
{
    Id(x => x.Id);
    HasOne(x => x.Staff).Cascade.All();
}

public EmployeeMap()
{
    Id(x => x.Id).GeneratedBy.Foreign("Store");
    HasOne(x => x.Store).Constrained();
}

问题出在哪里?现在,我们将拥有可以单独居住的Store.没关系.但是任何Employee都将需要分配Store.而且更重要的是... Employee将没有自己的ID列.实际上它将是可以命名为"StoreId"的列,因为ID值将来自Store.ID

Where is the Problem? Now we would have the Store which can live alone. That's ok. But any Employee will require a Store to be assigned to. And what is more important... the Employee won't have its own ID column. It will be in fact the column which could be nameed "StoreId", because the ID value would come from the Store.ID

这种情况非常罕见.实际上,在我看来,您已经拥有的映射非常好.

This scenario is very rare. And in fact, the mapping you already have seems to me pretty good.

我猜您正在尝试限制分配给StoreStaff数量.这可能是合理的,我们可以在Buisness层上实现这一目标.

I guess that you are trying to limit the amount of Staff assigned to the Store. This could be reasonable, and we can achieve that on the Buisness layer.

一个清晰(简化)的示例可能是这样的:

A clear (simplified) example could be something like this:

public class Store
{
    ...
    // not public
    protected virtual IList<Employee> Staff { get; set; }
    // here we do the trick on top of the mapping/hidden persistence layer
    public virtual Employee Employee 
    { 
       get { return Staff.FirstOrDefault(); }
       set { Staff[0] = value ; }

这篇关于nhibernate中的一对一映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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