Entity Framework Inheritance

SQL layer:

I have a table

enter image description here

Entity Chip Level:

I have the following rule: all offers that have a null state are outstanding offers, State is true - Accepted offers, State is false - rejected offers. In addition, some of the fields are used only for outstanding, partially - only for Accepted, etc. I use the first approach to the database, so I updated the EF model from the database and renamed the Offer object to OfferBase and created 3 child classes:

enter image description here

/ /. "" , Status = true ( Status is null) . Entity Framework? Accept, ( )

// 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();

OfferBase, :

'System.Data.Entity.DynamicProxies.OfferOutstanding_9DD3E4A5D716F158C6875FA0EDF5D0E52150A406416D4D641148F9AFE2B5A16A' "VTS.EFModels.OfferAccepted".

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

:

3 . : AcceptOffer, DeclineOffer OutstandingOffer.

AcceptOffer:

  • _
  • ContactID
  • FirstContactDate
  • LastContactDate
  • [... 5-10 ...]

DeclineOffer:

  • _
  • ContactID
  • [... 5-10 ...]

OutstandingOffer:

  • _
  • ContactID
  • FirstContactDate
  • LastContactDate
  • [... 5-10 ...]

? , , , ?

+4
2

. . , (, ), , ( ). .

, .

:

public class Offer
{
  public int Id { get; set; }
  public virtual OfferState State { get; set }
}

public class OfferState
{
  public int OfferId { get; set; }
  public string Notes { get; set; }
}

public class AcceptedOfferState : OfferState
{
  public DateTimeOffset AcceptDate { get; set; }
}

public class DeclinedOfferState : OfferState
{
  public DateTimeOffset DeclinedDate { get; set; }
}

, ; - ( PM EF): .

+6

, , #, , .

var accepteOffers= from i in _db.Offers
                   where i.ID == 1 && i.Status == true
                   select new OfferAccepted { AcceptDate = i.AcceptDate, StartTime = i.StartTime /* map all releaveant fields here */};
+3

All Articles