简体   繁体   English

多对一映射不允许属性

[英]Many-to-one mapping disallows property

This must be something simple somewhere I can't get hold of. 这一定是我无法掌握的简单事物。

Say a business offers multiple services. 假设一家企业提供多种服务。 Business on one side, ServiceOffers as lookup table and a BusinessServiceOffers for the association. 一方面是业务,将ServiceOffers作为查找表,将BusinessServiceOffers用于关联。 When I add a many-to-one mapping like below, sqlite provider(while unit testing) threw an argument out of range exception, understandably due to the duplicate reference in both Property and ManyToOne. 当我添加如下所示的多对一映射时,sqlite provider(在进行单元测试时)抛出了超出范围异常的参数,这可以理解是由于Property和ManyToOne中的重复引用。

Property(x => x.ServiceOfferingId);
ManyToOne
        (
            x => x.ServiceOffering,
            x =>
            {
                x.Fetch(FetchKind.Select);
                x.Lazy(LazyRelation.Proxy);
                x.Column("ServiceOfferingId");
                 x.ForeignKey("FK_BusinessServiceOffering_ServiceOfferingId");
            }
        );

Removing property fixed the exception issue but test kept failing. 删除属性可解决异常问题,但测试始终失败。 Looking into the inserted sql I see 'null' inserted for ServiceOfferingId instead of assigned int values like: 查看插入的sql,我看到为ServiceOfferingId插入了“ null”,而不是像这样分配的int值:

INSERT 
INTO
    BusinessServiceOffering
    (BusinessId, IsEnabled, CreatedDate, CreatedByLoginId, ModifiedDate, ModifiedByLoginId, ServiceOfferingId, BusinessServiceOfferingId) 
VALUES
    (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7);
@p0 = 200 [Type: Int32 (0)], @p1 = True [Type: Boolean (0)], 
@p2 = 2/28/2013 6:11:25 PM [Type: DateTime (0)], 
@p3 = 20 [Type: Int32 (0)], @p4 = 2/28/2013 6:11:25 PM [Type: DateTime (0)], 
@p5 = 30 [Type: Int32 (0)], @p6 = **NULL** [Type: Int32 (0)], --->
@p7 = 5 [Type: Int32 (0)]

I tried adding a bag on the "one" side but doesn't seem to make any difference. 我尝试在“一个”侧添加一个包,但似乎没有任何区别。

What am I missing? 我想念什么? Note that I have integration tests with sql server that run fine with or without the property. 请注意,我有与sql server的集成测试,无论有没有该属性,它都能正常运行。 However, simple Read tests with sql server fail to return any data for those fields where property is removed. 但是,使用sql server进行的简单读取测试无法为那些删除了属性的字段返回任何数据。

Seems like I need to map properly or it's a sqlite idiosyncrasy. 好像我需要正确地映射,或者这是一个sqlite特质。

Please help. 请帮忙。

Ok. 好。 Figured this one out myself and posting an answer for somebody else, should they get into same issue. 我自己弄清楚了这个问题,并在其他人遇到相同问题时发布了答案。

  1. The fields that were getting inserted as null were not supposed to be in POCO in the first place. 首先,不应将插入为null的字段放在POCO中。 They can be accessed if needed through the child/nested object and if relationships are mapped correctly. 如果需要,可以通过子对象/嵌套对象访问它们,并且可以正确映射关系。
  2. Inserting data into sqlite follow object reference rules. 将数据插入sqlite遵循对象引用规则。 For eg, in a parent child relation where child holds key reference to parent and parent has a list of children, create and save parent first and child next. 例如,在父子关系中,子项拥有对父项的关键引用,而父项具有子项列表,请首先创建并保存父项,然后创建并保存子项。 Mapping in this case is a bag/set of children in parent. 在这种情况下,映射是一袋/套父母的孩子。 There are other cases I'll update later. 还有其他情况,我稍后会更新。

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

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