[英]One to one mapping in NHibernate 3.2 mapping by code
I'm trying to learn NHibernate 3.2 built-in mapping by code api
( NOT Fluent NHibernate ).我正在尝试
NHibernate 3.2 built-in mapping by code api
( NOT Fluent NHibernate ) 学习NHibernate 3.2 built-in mapping by code api
。 Can you help me to map a one-to-one(or zero) relationship between these entities please?你能帮我映射这些实体之间的一对一(或零)关系吗?
NOTE: I googled the question, also I search the SOF, all examples are using Fluent API or XML;注意:我用谷歌搜索了这个问题,也搜索了 SOF,所有示例都使用 Fluent API 或 XML; I'm trying to use built-in mapping API in NHibernate 3.2
我正在尝试在 NHibernate 3.2 中使用内置映射 API
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; }
}
Primary key strategy is here:主键策略在这里:
Id(
t => t.Id,
t => {
t.Generator(Generators.HighLow, g => g.Params(new { max_low = 100 }));
t.Column(typeof(TEntity).Name + "Id");
});
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
如果您需要真正独特的双向约束,请参阅此一对一链接: http : //notherdev.blogspot.com/2012/01/mapping-by-code-onetoone.html
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.
在域和数据模型中将它们分开的典型方法是使用具有唯一约束的多对一连接到父级,这是一种常见模式。
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有关一般 3.2 映射的提示和提示,此资源对我有很大帮助: http : //notherdev.blogspot.com/2012/02/nhibernates-mapping-by-code-summary.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.