一对一映射在NHibernate 3.2中通过代码映射 [英] one to one mapping in NHibernate 3.2 mapping by code

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

问题描述

我试图学习 NHibernate 3.2内置映射的代码api 不流利的NHibernate )。

请注意:

NOTE: I googled the question, also I search the SOF, all examples are using Fluent API or xml; I'm trying to use built-in mapping api in NHibernate 3.2

public class Person { 
    public virtual int Id { get; set; }  
    public virtual string FirstName { get; set; } 
    public virtual string LastName { get; set; } 

    // can be null 
    public virtual Address Address { get; set; }
} 

public class Address { 
    public virtual int Id { get; set; } 
    public virtual string Line1 { get; set; } 
    public virtual string Line2 { get; set; } 
    public virtual string City { get; set; }

    // can not be null
    public virtual Person Person { get; set; } 
}

感谢先进。

UPDATE:

主要策略如下:

Id( 
    t => t.Id, 
    t => { 
        t.Generator(Generators.HighLow, g => g.Params(new { max_low = 100 })); 
        t.Column(typeof(TEntity).Name + "Id"); 
    });


推荐答案

根据实际需要, - 一个地图或多对一地图可以回答你的问题。如果您需要一个真正独特的双向约束,请参阅此链接一对一: http://notherdev.blogspot.com/2012/01/mapping-by-code-onetoone.html

Depending on what you ACTUALLY want- a one-to-one map or a many-to-one map may answer your question. Please see this link for one-to-one if you need a truly unique bi-directional constraint: http://notherdev.blogspot.com/2012/01/mapping-by-code-onetoone.html

to-ones通常是一个糟糕的策略,因为在几乎所有情况下将列放在一个表上是有意义的,如果您需要它们是您域中的独立实体,则通过组件映射将它们分开。在域和数据模型中分离它们的典型方法是使用具有唯一约束的多对一绑定到父对象,这是一个常见的模式。

One-to-ones are normally a bad strategy because it makes sense to just put the columns all on one table in almost all cases, and separate them via a component mapping if you need them to be separate entities in your domain. The typical way to separate them in the domain AND the data models is to use a many-to-one with a unique constraint tying back up to the parent and this is a common pattern.

对于一般3.2映射的提示和提示,这个资源对我有很多帮助: http://notherdev.blogspot.com/2012/02/nhibernates-mapping-by-code-summary.html

For tips and hints on general 3.2 mappings, this resource has been a ton of help for me: http://notherdev.blogspot.com/2012/02/nhibernates-mapping-by-code-summary.html

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

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