本文介绍了映射nhibernate父母/子女关系的“其他”方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用FluentNhibernate;
我试图坚持一个看似简单的对象模型:
public class Product
{
public int Id {get;组; }
public string Name {get;组; }
public Config {{get;组; }
}
public class Config
{
public int ConfigId {get;组; }
public int ProductId {get;组; }
public string ConfigField1 {get;组; }
public string ConfigField2 {get;组;这个数据库看起来像:(在语法上不正确) >
$ p $ CREATE TABLE [dbo]。[Products](
[ProductId] [int] IDENTITY(1,1)NOT NULL,
[Name] [varchar](50)NULL
)
创建表[dbo]。[Config](
[ConfigId] [int] IDENTITY(1, 1)NOT NULL,
[ProductId] [int] NOT NULL,
[ConfigField1] [varchar](50)NULL,
[ConfigField2] [varchar](50)NULL
)ON [PRIMARY]
开箱即用的fluent-nhibernate会尝试将其映射为外部键 Products
表,例如:
$ b
我不希望它这样做,而是希望映射先插入产品,然后再插入 ProductId
被插入到配置表中。
我把我的头发拉出来,尝试覆盖和阅读链接,如和(假设数据库存在)
我目前的流利映射是:
public class ProductOverrides:IAutoMappingOverride< Product>
public void Override(AutoMapping< Product> mapping)
{
mapping.Id(x => x.Id).Column(ProductId);
mapping.Table(Products);
}
}
public class ConfigOverrides:IAutoMappingOverride< Config>
{
public void Override(AutoMapping< Config> mapping)
{
mapping.Id(x => x.ConfigId);
$ div $解析方案
正试图通过映射多方的两倍来映射一对一的关系作为一对多的关系。这是行不通的。 NHibernate在它的一对一的定义是严格的,并要求双方都有相同的主键,所以也不会工作。
我有
using FluentNhibernate;
I am trying to persist a seemingly simple object model:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public Config Config { get; set; }
}
public class Config
{
public int ConfigId { get; set; }
public int ProductId { get; set; }
public string ConfigField1 { get; set; }
public string ConfigField2 { get; set; }
}
and the database looks like: (not syntactically correct)
CREATE TABLE [dbo].[Products](
[ProductId] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](50) NULL
)
CREATE TABLE [dbo].[Config](
[ConfigId] [int] IDENTITY(1,1) NOT NULL,
[ProductId] [int] NOT NULL,
[ConfigField1] [varchar](50) NULL,
[ConfigField2] [varchar](50) NULL
) ON [PRIMARY]
Out of the box fluent-nhibernate will try and map this as a foreign key on the Products
table, eg:
I don't want it to do this, rather I was hoping the mapping would insert Products first then the config second with ProductId
being inserted into the Config table.
I've pulled my hair out trying overrides and reading links such as this and this but still can't get it to do what I want. I am working with existing data and code so I would rather not change the database table definitions or the domain object. There is a bit more going on than what this example paints so if we could avoid discussions on domain model design that would be great. I have a link to a spike of this project here (assumes database exists)
my current fluent mappings are:
public class ProductOverrides : IAutoMappingOverride<Product>
{
public void Override(AutoMapping<Product> mapping)
{
mapping.Id(x => x.Id).Column("ProductId");
mapping.Table("Products");
}
}
public class ConfigOverrides : IAutoMappingOverride<Config>
{
public void Override(AutoMapping<Config> mapping)
{
mapping.Id(x => x.ConfigId);
}
}
解决方案
You are trying to map a one-to-one relationship as a one-to-many by mapping what would be the many side twice. That won't work. NHibernate is strict in its definition of one-to-one and requires that both sides have the same primary key, so that won't work either.
I've butted heads with this before and the best workaround I found is to model it as a standard on-to-many but only allow one item in the collection by encapsulating access to the it. In your case I would guess that Product would be the one side and Config the many.
这篇关于映射nhibernate父母/子女关系的“其他”方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!