2012-07-24 165 views
1

我正在使用实体框架代码优先方法。我有以下代码将数据插入到PaymentComponent和Payment表中。插入到PaymentComponent表中的数据不正确。即使域对象中的相应属性不为空,它也会在两列(对于一条记录)中具有NULL值。需要改变什么才能使其工作?实体框架代码优先:插入数据库的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; } 

} 

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. 实体框架映射方案 - 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

回答

3

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

+0

谢谢。这是否意味着在EF中不支持重写属性? – Lijo 2012-07-24 08:44:09

3

还没有定义的的DataContext ClubCardPayment dbset。

插入此,它应该工作

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

我已经更新了这个问题。即使这样,我也会得到意想不到的结果。请参阅最新的问题。 – Lijo 2012-07-24 08:39:10

0

您需要定义2类,实际上是实现了抽象类中,这是唯一的办法EF会知道不同类别,以及如何读取/更新/编写它们的实例。

(无需映射EF中的抽象类!)。

+0

我有两个类实现抽象基类 - GiftCouponPayment和ClubCardPayment。 – Lijo 2012-07-24 08:41:06

0

这不会对您的问题有所贡献,而只是来自我身边的提示:
为什么在您的派生类中实现MyValue和MyType?你可以把它放到抽象类中,如果执行总是一样的话......

+0

实施可能会在未来发生变化。我只是为了简单起见。 – Lijo 2012-07-24 08:43:18