2

I am using Entity Framework Code First Approach. I have following code to insert data into PaymentComponent and Payment tables. The data getting inserted into PaymentComponent table is not proper. It has NULL values in two columns (for one record) even though the corresponding properties in the domain objects are not null. What need to be changed in order to make it working?

enter image description here

EDIT

When I added the following in NerdDinners class, I am getting following result - it has new unwanted columns

  public DbSet<ClubCardPayment> ClubCardPayments { get; set; }

enter image description here

ORIGINAL CODE

static void Main(string[] args)
{
    string connectionstring = "Data Source=.;Initial Catalog=NerdDinners;Integrated Security=True;Connect Timeout=30";

    using (var db = new NerdDinners(connectionstring))
    {
        GiftCouponPayment giftCouponPayment = new GiftCouponPayment();
        giftCouponPayment.MyValue=250;
        giftCouponPayment.MyType = "GiftCouponPayment";

        ClubCardPayment clubCardPayment = new ClubCardPayment();
        clubCardPayment.MyValue = 5000;
        clubCardPayment.MyType = "ClubCardPayment";


        List<PaymentComponent> comps = new List<PaymentComponent>();
        comps.Add(giftCouponPayment);
        comps.Add(clubCardPayment);

        var payment = new Payment { PaymentComponents = comps, PayedTime=DateTime.Now };
        db.Payments.Add(payment);

        int recordsAffected = db.SaveChanges();

    }
}

DOMAIN CODE

public abstract class PaymentComponent
{
    public int PaymentComponentID { get; set; }
    public abstract int MyValue { get; set; }
    public abstract string MyType { get; set; }
    public abstract int GetEffectiveValue();
}


public partial class GiftCouponPayment : PaymentComponent
{

    private int couponValue;
    private string myType;

    public override int MyValue
    {
        get
        {
            return this.couponValue;
        }
        set
        {
            this.couponValue = value;
        }
    }

    public override string MyType
    {
        get
        {
            return this.myType;
        }
        set
        {
            this.myType = value;
        }
    }

    public override int GetEffectiveValue()
    {
        if (this.PaymentComponentID < 2000)
        {
            return 0;
        }
        return this.couponValue;
    }

}


public partial class ClubCardPayment : PaymentComponent
{

    private int cardValue;
    private string myType;

    public override int MyValue
    {
        get
        {
            return this.cardValue;
        }
        set
        {
            this.cardValue = value;
        }
    }

    public override string MyType
    {
        get
        {
            return this.myType;
        }
        set
        {
            this.myType = value;
        }
    }

    public override int GetEffectiveValue()
    {
        return this.cardValue;
    }

}

public partial class Payment
{
    public int PaymentID { get; set; }
    public List<PaymentComponent> PaymentComponents { get; set; }
    public DateTime PayedTime { get; set; }

}



//System.Data.Entity.DbContext is from EntityFramework.dll
public class NerdDinners : System.Data.Entity.DbContext
{

    public NerdDinners(string connString): base(connString)
    { 

    }

    protected override void OnModelCreating(DbModelBuilder modelbuilder)
    {
        modelbuilder.Conventions.Remove<PluralizingTableNameConvention>();
    }


    public DbSet<GiftCouponPayment> GiftCouponPayments { get; set; }
    public DbSet<Payment> Payments { get; set; }

}

REFERENCE:

  1. When using entity framework code-first mapping property to separate table, moves foreign key field
  2. Override Entity Framework Entity Property
  3. EntityFramework how to Override properties
  4. http://weblogs.asp.net/manavi/archive/2011/04/24/associations-in-ef-4-1-code-first-part-4-table-splitting.aspx
  5. http://www.robbagby.com/entity-framework/entity-framework-modeling-entity-splitting/
  6. Entity Framework Mapping Scenarios - http://msdn.microsoft.com/en-us/library/cc716779.aspx
  7. http://blogs.microsoft.co.il/blogs/gilf/archive/2009/03/06/entity-splitting-in-entity-framework.aspx

4 Answers 4

3

Implement MyType and MyValue directly in the base class. EF allows shared members to be implemented only in the base class. Members implemented in derived class use their own columns in the resulting table.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Does it mean that overriding of properties is not supported in EF?
3

you haven't defined the ClubCardPayment dbset in the datacontext.

insert this and it should work

public DbSet<ClubCardPayment> ClubCardPayments { get; set; }

Comments

0

You need to define the 2 classes that are actually implements of the abstract class, that's the only way EF will know the different classes and how to read/update/write instances of them.

(No need to map the abstract class in EF!).

1 Comment

I have two classes that implement abstract base class - GiftCouponPayment and ClubCardPayment.
0

This doesn't contribute to your question, but just a hint from my side:
Why do you implement MyValue and MyType explcitly in your derived classes? You can just put it into the abstract class, if the implementation is always the same...

1 Comment

The implementation can change in future. I just put it so for simplicity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.