【问题标题】:Entity Framework: Split table into multiple tables实体框架:将表拆分为多个表
【发布时间】:2013-04-10 04:21:18
【问题描述】:

我使用以下 EF 代码优先方法(TPH 继承)创建了下表 [PaymentComponent]。它工作正常。我需要更改数据库设计——需要将 GiftCouponPayments 存储在 GiftCouponPayment 表中,并将 ClubCardPayments 存储在 ClubCardPayment 表中。需要对 C# 代码进行哪些更改才能获得所需的数据库结构?

代码

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

}

客户

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

}

参考

  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

【问题讨论】:

  • @LadislavMrnka 您对以下 EF 表拆分 - 实体拆分的对立面 - thedatafarm.com/blog/data-access/… 有何建议?
  • 在您想使用派生实体类型的情况下,表拆分对您没有帮助。

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


【解决方案1】:

在 OnModelCreating 的 Context 类中:

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

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

【讨论】:

  • 有了这个。我收到异常 - “AcceptChanges 无法继续,因为对象的键值与 ObjectStateManager 中的另一个对象冲突。在调用 AcceptChanges 之前,请确保键值是唯一的。”有什么想法吗?
  • This 可能会有所帮助。
猜你喜欢
  • 1970-01-01
  • 2017-12-02
  • 1970-01-01
  • 1970-01-01
  • 2015-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多