Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8268421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:52:56+00:00 2026-06-08T05:52:56+00:00

I am using Entity Framework Code First Approach. I have following code to insert

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T05:52:58+00:00Added an answer on June 8, 2026 at 5:52 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following table created using Entity Framework Code First approach. How do
I have following C# code that uses Entity Framework Code First approach. The tables
I am using the code-first approach in Entity Framework 4.0. I have defined an
I'm using Entity Framework Code First v 4.3.0 I have a two entities with
I am Using C# 2010 Entity framework code first If I have A class
I have been using Entity Framework CTP with Code-First as in this tutorial by
I am successfully creating database (SQL Ce) using Entity Framework Code First approach (C#-WPF).
I have an ASP.NET MVC 3 application using an Entity Framework (4.3.1) Code First
I'm building an ASP.NET MVC 3 site using the code-first Entity Framework 4 approach.
I'm using the Entity Framework 4 code first approach to design my database in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.