实体框架:分割表到多个表 [英] Entity Framework: Split table into multiple tables

查看:149
本文介绍了实体框架:分割表到多个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下表[PaymentComponent]使用下面的EF code第一种方法(TPH继承)创建的。它工作正常。我需要改变数据库设计 - 需要存储在GiftCouponPayment表GiftCouponPayments和ClubCardPayments在ClubCardPayment表。需要什么样的变化将在C#code进行,以获得所需的数据库结构?

code

 公共抽象类PaymentComponent
{
    公众诠释PaymentComponentID {获得;组; }
    公众诠释myvalue的{获得;组; }
    公共字符串的MyType {获得;组; }
    公共抽象INT GetEffectiveValue();
}


公共部分类GiftCouponPayment:PaymentComponent
{

    公众覆盖INT GetEffectiveValue()
    {
        如果(myvalue的&所述; 2000)
        {
            返回0;
        }
        返回myvalue的;
    }

}


公共部分类ClubCardPayment:PaymentComponent
{
    公众覆盖INT GetEffectiveValue()
    {
        返回myvalue的;
    }
}

公共部分类付款
{
    公众诠释PaymentID {获得;组; }
    公开名单< PaymentComponent> PaymentComponents {获得;组; }
    公开日期时间PayedTime {获得;组; }

}



//System.Data.Entity.DbContext是EntityFramework.dll
公共类NerdDinners:System.Data.Entity.DbContext
{

    公共NerdDinners(字符串CONNSTRING):基地(CONNSTRING)
    {

    }

    保护覆盖无效OnModelCreating(DbModelBuilder模型构建器)
    {
        modelbuilder.Conventions.Remove&其中; PluralizingTableNameConvention>();
    }


    公共DbSet< GiftCouponPayment> GiftCouponPayments {获得;组; }
    公共DbSet< ClubCardPayment> ClubCardPayments {获得;组; }
    公共DbSet<付款和GT;付款{获得;组; }

}
 

客户端

 静态无效的主要(字串[] args)
{
    字符串的ConnectionString =数据源=;初始目录= NerdDinners;集成安全性= TRUE;连接超时= 30;

    使用(VAR DB =新NerdDinners(的ConnectionString))
    {
        GiftCouponPayment giftCouponPayment =新GiftCouponPayment();
        giftCouponPayment.MyValue = 250;
        giftCouponPayment.MyType =GiftCouponPayment;

        ClubCardPayment clubCardPayment =新ClubCardPayment();
        clubCardPayment.MyValue = 5000;
        clubCardPayment.MyType =ClubCardPayment;

        名单< PaymentComponent>谱曲=新的名单,其中,PaymentComponent>();
        comps.Add(giftCouponPayment);
        comps.Add(clubCardPayment);

        VAR支付=新的支付{PaymentComponents =谱曲,PayedTime = DateTime.Now};
        db.Payments.Add(付款);

        INT recordsAffected = db.SaveChanges();
    }

}
 

参考

  1. <一个href="http://stackoverflow.com/questions/9511021/how-do-i-get-entity-framework-4-3-$c$c-first-to-map-a-subclass-using-table-per-t">How我得到实体框架4.3 code首先要使用每个类型(TPT)映射一个子类?
  2. <一个href="http://weblogs.asp.net/manavi/archive/2011/04/24/associations-in-ef-4-1-$c$c-first-part-4-table-splitting.aspx" rel="nofollow">http://weblogs.asp.net/manavi/archive/2011/04/24/associations-in-ef-4-1-$c$c-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 /库/ cc716779.aspx
  5. <一个href="http://blogs.microsoft.co.il/blogs/gilf/archive/2009/03/06/entity-splitting-in-entity-framework.aspx" rel="nofollow">http://blogs.microsoft.co.il/blogs/gilf/archive/2009/03/06/entity-splitting-in-entity-framework.aspx
解决方案

在上下文类OnModelCreating:

  modelBuilder.Entity&LT; GiftCouponPayment&GT;()
                .MAP(米=&GT;
                {
                    m.MapInheritedProperties();
                    m.ToTable(GiftCouponPayment);
                });

modelBuilder.Entity&LT; ClubCardPayment&GT;()
                .MAP(米=&GT;
                {
                    m.MapInheritedProperties();
                    m.ToTable(ClubCardPayment);
                });
 

I have the following table [PaymentComponent] created using following EF code first approach (TPH inheritance). It works fine. I need to change the database design – need to store GiftCouponPayments in GiftCouponPayment table and ClubCardPayments in ClubCardPayment table. What change need to be done in C# code to get the required database structure?

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. Entity Framework Mapping Scenarios - 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

解决方案

In your Context class in OnModelCreating:

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

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

这篇关于实体框架:分割表到多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆