2012-07-24 111 views
1

我使用以下EF代码第一种方法(TPH继承)创建了下表[PaymentComponent]。它工作正常。我需要更改数据库设计 - 需要将GiftCouponPayments存储在GiftCouponPayment表和ClubCardPayment表中的ClubCardPayments中。在C#代码中需要做什么修改才能获得所需的数据库结构?实体框架:将表拆分为多个表

enter image description here

CODE

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


public partial class GiftCouponPayment : PaymentComponent 
{ 

    public override int GetEffectiveValue() 
    { 
     if (MyValue < 2000) 
     { 
      return 0; 
     } 
     return MyValue; 
    } 

} 


public partial class ClubCardPayment : PaymentComponent 
{ 
    public override int GetEffectiveValue() 
    { 
     return MyValue; 
    } 
} 

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<ClubCardPayment> ClubCardPayments { get; set; } 
    public DbSet<Payment> Payments { get; set; } 

} 

CLIENT

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

} 

REFERENCE

  1. How do I get Entity Framework 4.3 Code First to map a subclass using Table Per Type (TPT)?
  2. http://weblogs.asp.net/manavi/archive/2011/04/24/associations-in-ef-4-1-code-first-part-4-table-splitting.aspx
  3. http://www.robbagby.com/entity-framework/entity-framework-modeling-entity-splitting/
  4. 实体框架映射方案 - http://msdn.microsoft.com/en-us/library/cc716779.aspx
  5. http://blogs.microsoft.co.il/blogs/gilf/archive/2009/03/06/entity-splitting-in-entity-framework.aspx
+1

海用于TPC继承。 – 2012-07-24 09:40:18

+0

@LadislavMrnka你对下面的EF表分裂有什么建议? - 实体分裂的对立面 - http://thedatafarm.com/blog/data-access/ef-table-splitting-ndash-the-opposite-of-entity-分裂/? – Lijo 2012-07-24 11:07:16

+1

在您想要使用派生实体类型的场景中,表分割不会对您有所帮助。 – 2012-07-24 11:53:50

回答

1

在上下文类OnModelCreating:

modelBuilder.Entity<GiftCouponPayment>() 
       .Map(m => 
       { 
        m.MapInheritedProperties(); 
        m.ToTable("GiftCouponPayment"); 
       }); 

modelBuilder.Entity<ClubCardPayment>() 
       .Map(m => 
       { 
        m.MapInheritedProperties(); 
        m.ToTable("ClubCardPayment"); 
       }); 
+0

用这个。我收到异常 - “AcceptChanges无法继续,因为对象的键值与ObjectStateManager中的另一个对象发生冲突。请在调用AcceptChanges之前确保键值是唯一的。有什么想法吗? – Lijo 2012-07-24 10:42:49

+1

[This](http://blogs.msdn.com/b/diego/archive/2010/10/06/self-tracking-entities-applychanges-and-duplicate-entities.aspx)可能会有帮助。 – 2012-07-24 11:06:31