简体   繁体   English

nhibernate中的一对一映射

[英]one to one mapping in nhibernate

I am using nhibernate. 我正在使用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>();
        }
    }

Employee class 员工阶层

    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; }
        }

storemap storemap

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

employeemap EmployeeMap的

public EmployeeMap ()
        {
            Id(x => x.Id);
            Map(x => x.FirstName);
            Map(x => x.LastName);
            References(x => x.Store  ).WithColumns("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"); 在employeemap引用中(x => x.Store).WithForeignKey(“ store”); and in stormap 并在暴风雨中

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

but this result in following error 但这导致以下错误

persistent class not known: 持久类未知:

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

Please help 请帮忙

The one-to-one mapping is very specific. one-to-one映射非常具体。 It says, that one object cannat exist without the other. 它说,一个对象不能存在而另一个则不能存在。 Check docs 5.1.11. 检查文档5.1.11。 one-to-one and this for fluent How to do a fluent nhibernate one to one mapping? 一对一 ,这对于流利而言如何使流利的休眠一对一映射? .

We can make it as an example liek this: 我们可以举个例子:

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();
}

Where is the Problem? 问题出在哪儿? Now we would have the Store which can live alone. 现在我们有了可以单独居住的Store That's ok. 没关系。 But any Employee will require a Store to be assigned to. 但是任何Employee都将需要分配Store And what is more important... the Employee won't have its own ID column. 还有更重要的是... Employee将没有自己的ID列。 It will be in fact the column which could be nameed "StoreId", because the ID value would come from the Store.ID 实际上它将是可以命名为“ StoreId”的列,因为ID值将来自Store.ID

suggestion 建议

This scenario is very rare. 这种情况非常罕见。 And in fact, the mapping you already have seems to me pretty good. 实际上,在我看来,您已经拥有的映射非常好。

I guess that you are trying to limit the amount of Staff assigned to the Store . 我猜您正在尝试限制分配给该StoreStaff数量。 This could be reasonable, and we can achieve that on the Buisness layer. 这可能是合理的,我们可以在Buisness层上实现这一目标。

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 ; }

A list virtual IList<Employee> Staff cannot map to HasOne 列表virtual IList<Employee> Staff无法映射到HasOne

Simply make it not to be a list should work ;) 只需使其不成为列表即可;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM