locked
Entity Framework inheritance RRS feed

  • Question

  • User3112162 posted

    SQL Layer:

    I have a table

    Entity Framwork Layer:

    I have 3 types of Offer entity. There are: AcceptOffer, DeclineOffer and OutstandingOffer.

    I have the following rule: all Offers, which have State is null, are Outstanding offers, State is true are Accepted offers, State is false are Declined offers. Also, part of fields used only for Outstanding, part - only for Accepted etc... I use Database first approach, so, I updated EF model from DB and renamed Offer entity to OfferBase and created 3 child classes:

    AcceptOffer:

    •  UserID
    •  ContactID
    •  Notes
    •  FirstContactDate
    •  LastContactDate 
    •  [... and 5-10 the unique fields...]

    DeclineOffer:

    •  UserID
    •  ContactID
    •  Notes 
    •  [... and 5-10 the unique fields...]

    OutstandingOffer:

    •  UserID
    •  ContactID
    •  FirstContactDate
    •  LastContactDate 
    •  [... and 5-10 the unique fields...]

    it works fine for add/select entities/records. Right now I want to "move" offer from outstanding to accept offer, so, I need to set Status=true (from Status is null) for appropriate record. But how to do it by Entity Framework? If I try to select outstanding offer as Accept offer I get an null reference (and clearly why)

    // record with ID=1 exists, but State is null, so, EF can not find this record and offer will be null after the following string
    var offer = (from i in _db.OfferBases.OfType<EFModels.OfferAccepted>() where i.ID == 1 select i).FirstOrDefault();


    if I try to select as OfferBase entity I get the following error:

    > Unable to cast object of type
    > 'System.Data.Entity.DynamicProxies.OfferOutstanding_9DD3E4A5D716F158C6875FA0EDF5D0E52150A406416D4D641148F9AFE2B5A16A'
    > to type 'VTS.EFModels.OfferAccepted'.

    var offerB = (from i in _db.OfferBases where i.ID == 1 select i).FirstOrDefault();
    var offer = (EFModels.OfferAccepted)offerB;

    How to do it correctly? Of course, I can select a record, remove from DB and add new with appropriate state value, but how to do it normally?



    Friday, March 20, 2015 3:18 PM

All replies

  • User1644755831 posted

    Hello rover83,

    You are trying to cast a parent class to child class. it is not possible to cast parent type to a child class.

    Read this to understand more.

    rover83

    How to do it correctly? Of course, I can select a record, remove from DB and add new with appropriate state value, but how to do it normally?

    Event if in entity framework you have parent and child models in database clearly they are separate tables so I believe you when you select a record, remove from DB and add new with appropriate state value that is the normal way. Because you are trying  to "move" offer from outstanding to accept offer in terms of database you are trying to delete a record from one table and add it to another table.

    Hope this helps.

    With Regards,

    Krunal Parekh

    Sunday, March 29, 2015 12:41 AM
  • User3112162 posted

    Event if in entity framework you have parent and child models in database clearly they are separate tables so I believe you when you select a record, remove from DB and add new with appropriate state value that is the normal way. Because you are trying  to "move" offer from outstanding to accept offer in terms of database you are trying to delete a record from one table and add it to another table.

    as you can see, DB has one table. EF model has 4 classes (one parent and 3 child) :)

    Tuesday, April 14, 2015 6:17 PM