简体   繁体   中英

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. 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.

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:

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. However, simple Read tests with sql server fail to return any data for those fields where property is removed.

Seems like I need to map properly or it's a sqlite idiosyncrasy.

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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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