c# - Entity Framework 代码优先 : NULL values inserted in database

标签 c# .net entity-framework ef-code-first

我正在使用 Entity Framework 代码优先方法。我有以下代码将数据插入 PaymentComponent 和 Payment 表中。插入 PaymentComponent 表的数据不正确。即使域对象中的相应属性不为空,它的两列(对于一条记录)也有 NULL 值。需要更改什么才能使其正常工作?

enter image description here

编辑

当我在 NerdDinners 类中添加以下内容时,我得到以下结果 - 它有新的不需要的列

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

enter image description here

原始代码

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

    }
}

域名代码

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; }

}

引用:

  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 映射场景 - 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

最佳答案

直接在基类中实现MyTypeMyValue。 EF 允许仅在基类中实现共享成员。在派生类中实现的成员在结果表中使用它们自己的列。

关于c# - Entity Framework 代码优先 : NULL values inserted in database,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11627031/

相关文章:

c# - 从 C# 访问 Firefox

.net - ASP.net 成员资格 - 添加角色

c# - 看似有效的 JSON Blob 的 JSON.NET 反序列化失败

c# - 将嵌套的 foreach 编写为 LINQ 以聚合两个单独的值

c# - EF 4.1 Code First - 确定更改了哪些属性

c# - 比较 C# 中的两个 List<enum> 对象

c# - 如果我在处理 WCF 请求时停止我的 Windows 服务,会发生什么情况?

时间:2019-03-24 标签:c#datetimeformat(yyyy-mm-ddT00 :00:00) for google chart date

c# - 使用值属性作为键的字典

c# - 具有多个引用的 Entity Framework LoadProperty