LCJ
LCJ

Reputation: 22662

Data does not get inserted: EF Code First

I have following code to insert data into GiftCouponPayment table and Payment table. This code successfully created a database and these two tables. However there is no data inserted in one table - GiftCouponPayment table. What need to be changed in order to make it working?

enter image description here

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))
        {
            var giftCouponPayment = new GiftCouponPayment();
            giftCouponPayment.MyID=1;
            giftCouponPayment.MyValue=250;


            List<IPaymentComponent> comps = new List<IPaymentComponent>();
            comps.Add(giftCouponPayment);
            var payment = new Payment { PaymentComponents = comps, PaymentID = 1, PayedTime=DateTime.Now };
            db.Payments.Add(payment);

            int recordsAffected = db.SaveChanges();


        }

}

Domain Classes

namespace LijosEF
{

public interface IPaymentComponent
{
    int MyID { get; set; }
    int MyValue { get; set; }
    int GetEffectiveValue();
}


public partial class GiftCouponPayment : IPaymentComponent
{

    private int CouponValue;
    public int MyID
    {
        get
        {
            return this.GiftCouponPaymentID;
        }
        set
        {
            this.GiftCouponPaymentID = value;
        }
    }

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

    public int GetEffectiveValue()
    {
        if (this.GiftCouponPaymentID < 2000)
        {
            return 0;
        }
        return this.CouponValue;
    }

    public int GiftCouponPaymentID { get; set; }

}

public partial class Payment
{
    public int PaymentID { get; set; }
    public List<IPaymentComponent> 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; }

}
}

Upvotes: 0

Views: 147

Answers (1)

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364349

You cannot use interface in your navigation property - EF doesn't support it. You must declare your payment directly with a class:

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

If your Payment can have different PaymentComponents you must use mapped inheritance with abstract base class instead of interface.

Upvotes: 1

Related Questions