none
EF 4.1 TPT com colunas PK de nomes diferentes RRS feed

  • Pergunta

  • Pessoal, bom dia!

    Estou tentando implementar o EF aqui na minha empresa, mas estou com alguns problemas.

    Tenho as seguintes classes POCOs:

        public abstract class CommissionGoal
        {
            public int Id { get; set; }
            public DateTime StartDate { get; set; }
            public DateTime ExpireDate { get; set; }
            public DateTime InsertDate { get; set; }
            public DateTime UpdateDate { get; set; }
        }
    
        public class CommissionGoalByMonth : CommissionGoal
        {
            public int Min { get; set; }
            public int Max { get; set; }
            public decimal MaxPercent { get; set; }
            public decimal Factor { get; set; }
            public int DayLimit { get; set; }
            public int LimitChange { get; set; }
        }
    
        public class CommissionGoalBySales : CommissionGoal
        {
            public int Amount { get; set; }
            public decimal Factor { get; set; }
        }

    Essas classes seriam representadas pelas seguintes tabelas:

    CREATE TABLE [dbo].[CommissionGoal](
    	[CmmGoal_Id] [int] IDENTITY(1,1) NOT NULL,
    	[CmmGoal_StartDate] [datetime] NOT NULL,
    	[CmmGoal_ExpireDate] [datetime] NOT NULL,
    	[CmmGoal_InsertDate] [datetime] NOT NULL,
    	[CmmGoal_UpdateDate] [datetime] NOT NULL,
     CONSTRAINT [PK_CommissionGoal] PRIMARY KEY CLUSTERED 
    (
    	[CmmGoal_Id] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    GO
    
    CREATE TABLE [dbo].[CommissionGoalBySales](
    	[CmmGoalSls_Id] [int] NOT NULL,
    	[CmmGoalSls_Amount] [int] NOT NULL,
    	[CmmGoalSls_Factor] [decimal](18, 4) NOT NULL,
     CONSTRAINT [PK_CommissionGoalBySales] PRIMARY KEY CLUSTERED 
    (
    	[CmmGoalSls_Id] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    GO
    
    CREATE TABLE [dbo].[CommissionGoalByMonth](
    	[CmmGoalMth_Id] [int] NOT NULL,
    	[CmmGoalMth_Min] [int] NOT NULL,
    	[CmmGoalMth_Max] [int] NOT NULL,
    	[CmmGoalMth_MaxPercent] [decimal](18, 4) NOT NULL,
    	[CmmGoalMth_Factor] [decimal](18, 4) NOT NULL,
    	[CmmGoalMth_DayLimit] [int] NOT NULL,
    	[CmmGoalMth_LimitChange] [int] NOT NULL,
     CONSTRAINT [PK_CommissionGoalByMonth] PRIMARY KEY CLUSTERED 
    (
    	[CmmGoalMth_Id] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    GO
    
    ALTER TABLE [dbo].[CommissionGoalByMonth]  WITH CHECK ADD  CONSTRAINT [FK_CommissionGoalByMonth_CommissionGoal] FOREIGN KEY([CmmGoalMth_Id])
    REFERENCES [dbo].[CommissionGoal] ([CmmGoal_Id])
    GO
    
    ALTER TABLE [dbo].[CommissionGoalBySales]  WITH CHECK ADD  CONSTRAINT [FK_CommissionGoalBySales_CommissionGoal] FOREIGN KEY([CmmGoalSls_Id])
    REFERENCES [dbo].[CommissionGoal] ([CmmGoal_Id])
    GO

    Como eu faço para realizar esse mapeamento utilizando EntityTypeConfiguration?

    *Obs.: Necessito utilizar EntityTypeConfiguration pois estamos configurando na ferramenta um DbContext que irá carregar todos os EntityTypeConfiguration do nosso relacionamento (eles estão anotados). Não queremos colocar nas nossas classes POCOs as anotaçoes do EF pois elas são usadas para outros itens (sendo assim, preferimos não misturar as coisas).

    No aguardo e obrigado!

    Rafael


    • Editado Rafael Balconi terça-feira, 22 de maio de 2012 14:09
    • Movido Wagner dos Santos Vasconcellos quarta-feira, 23 de maio de 2012 10:39 (De:Arquitetura de Soluções)
    • Movido Fábio Jr quarta-feira, 23 de maio de 2012 11:21 Não é script (De:Scripts Administrativos)
    terça-feira, 22 de maio de 2012 14:07

Respostas

  • Olá Rafael,

    Montei a estrutura para vc. Testei aqui e funcionou.

    []s!

    class Program
    {
        static void Main(string[] args)
        {
            string connectionString = @"User ID=;pwd=;Initial Catalog=msdnSamples;Data Source=.";
    
            using (Context context = new Context(connectionString))
            {
                CommissionGoalByMonth c = new CommissionGoalByMonth();
                c.DayLimit = 0;
                c.ExpireDate = DateTime.Now;
                c.Factor = 10;
                c.InsertDate = DateTime.Now;
                c.LimitChange = 10;
                c.Max = 10;
                c.MaxPercent = 10;
                c.Min = 10;
                c.StartDate = DateTime.Now;
                c.UpdateDate = DateTime.Now;
    
                context.Commissions.Add(c);
    
                context.SaveChanges();
            }
        }
    }
    
    public class CommissionGoalBySalesConfig : EntityTypeConfiguration<CommissionGoalBySales>
    {
        public CommissionGoalBySalesConfig()
        {
            this.HasKey(m => m.Id);
            this.Property(m => m.Id).HasColumnName("CmmGoalSls_Id");
            this.Property(m => m.Amount).HasColumnName("CmmGoalSls_Amount");
            this.Property(m => m.Factor).HasColumnName("CmmGoalSls_Factor");
        }
    }
    
    public class CommissionGoalByMonthConfig : EntityTypeConfiguration<CommissionGoalByMonth>
    {
        public CommissionGoalByMonthConfig()
        {
            this.HasKey(m => m.Id);
            this.Property(m => m.Id).HasColumnName("CmmGoalMth_Id");
            this.Property(m => m.Min).HasColumnName("CmmGoalMth_Min");
            this.Property(m => m.Max).HasColumnName("CmmGoalMth_Max");
            this.Property(m => m.MaxPercent).HasColumnName("CmmGoalMth_MaxPercent");
            this.Property(m => m.Factor).HasColumnName("CmmGoalMth_Factor");
            this.Property(m => m.DayLimit).HasColumnName("CmmGoalMth_DayLimit");
            this.Property(m => m.LimitChange).HasColumnName("CmmGoalMth_LimitChange");
        }
    }
    
    public class CommissionGoalConfig : EntityTypeConfiguration<CommissionGoal>
    {
        public CommissionGoalConfig()
        {
            this.HasKey(m => m.Id);
            this.Property(m => m.Id).HasColumnName("CmmGoal_Id");
            this.Property(m => m.StartDate).HasColumnName("CmmGoal_StartDate");
            this.Property(m => m.ExpireDate).HasColumnName("CmmGoal_ExpireDate");
            this.Property(m => m.InsertDate).HasColumnName("CmmGoal_InsertDate");
            this.Property(m => m.UpdateDate).HasColumnName("CmmGoal_UpdateDate");
    
            this.Map(c =>
            {
                c.ToTable("CommissionGoal");
                c.MapInheritedProperties();
            })
            .Map<CommissionGoalByMonth>(c =>
            {
                c.ToTable("CommissionGoalByMonth");
            })
            .Map<CommissionGoalBySales>(c =>
            {
                c.ToTable("CommissionGoalBySales");
            });
        }
    }
    
    public class Context : DbContext
    {
        public Context(string connectionString)
            : base(connectionString)
        {
            Database.SetInitializer<Context>(null);
        }
    
        public DbSet<CommissionGoal> Commissions { get; set; }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new CommissionGoalByMonthConfig());
            modelBuilder.Configurations.Add(new CommissionGoalBySalesConfig());
            modelBuilder.Configurations.Add(new CommissionGoalConfig());
        }
    }
    
    public abstract class CommissionGoal
    {
        public int Id { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime ExpireDate { get; set; }
        public DateTime InsertDate { get; set; }
        public DateTime UpdateDate { get; set; }
    }
    
    public class CommissionGoalByMonth : CommissionGoal
    {
        public int Min { get; set; }
        public int Max { get; set; }
        public decimal MaxPercent { get; set; }
        public decimal Factor { get; set; }
        public int DayLimit { get; set; }
        public int LimitChange { get; set; }
    }
    
    public class CommissionGoalBySales : CommissionGoal
    {
        public int Amount { get; set; }
        public decimal Factor { get; set; }
    }


    Fernando Henrique Inocêncio Borba Ferreira
    while(alive){ this.WriteCode(); }
    Blog: http://ferhenriquef.com/
    Twitter: @ferhenrique


    quarta-feira, 23 de maio de 2012 15:11
    Moderador

Todas as Respostas

  • Olá Rafael,

    Montei a estrutura para vc. Testei aqui e funcionou.

    []s!

    class Program
    {
        static void Main(string[] args)
        {
            string connectionString = @"User ID=;pwd=;Initial Catalog=msdnSamples;Data Source=.";
    
            using (Context context = new Context(connectionString))
            {
                CommissionGoalByMonth c = new CommissionGoalByMonth();
                c.DayLimit = 0;
                c.ExpireDate = DateTime.Now;
                c.Factor = 10;
                c.InsertDate = DateTime.Now;
                c.LimitChange = 10;
                c.Max = 10;
                c.MaxPercent = 10;
                c.Min = 10;
                c.StartDate = DateTime.Now;
                c.UpdateDate = DateTime.Now;
    
                context.Commissions.Add(c);
    
                context.SaveChanges();
            }
        }
    }
    
    public class CommissionGoalBySalesConfig : EntityTypeConfiguration<CommissionGoalBySales>
    {
        public CommissionGoalBySalesConfig()
        {
            this.HasKey(m => m.Id);
            this.Property(m => m.Id).HasColumnName("CmmGoalSls_Id");
            this.Property(m => m.Amount).HasColumnName("CmmGoalSls_Amount");
            this.Property(m => m.Factor).HasColumnName("CmmGoalSls_Factor");
        }
    }
    
    public class CommissionGoalByMonthConfig : EntityTypeConfiguration<CommissionGoalByMonth>
    {
        public CommissionGoalByMonthConfig()
        {
            this.HasKey(m => m.Id);
            this.Property(m => m.Id).HasColumnName("CmmGoalMth_Id");
            this.Property(m => m.Min).HasColumnName("CmmGoalMth_Min");
            this.Property(m => m.Max).HasColumnName("CmmGoalMth_Max");
            this.Property(m => m.MaxPercent).HasColumnName("CmmGoalMth_MaxPercent");
            this.Property(m => m.Factor).HasColumnName("CmmGoalMth_Factor");
            this.Property(m => m.DayLimit).HasColumnName("CmmGoalMth_DayLimit");
            this.Property(m => m.LimitChange).HasColumnName("CmmGoalMth_LimitChange");
        }
    }
    
    public class CommissionGoalConfig : EntityTypeConfiguration<CommissionGoal>
    {
        public CommissionGoalConfig()
        {
            this.HasKey(m => m.Id);
            this.Property(m => m.Id).HasColumnName("CmmGoal_Id");
            this.Property(m => m.StartDate).HasColumnName("CmmGoal_StartDate");
            this.Property(m => m.ExpireDate).HasColumnName("CmmGoal_ExpireDate");
            this.Property(m => m.InsertDate).HasColumnName("CmmGoal_InsertDate");
            this.Property(m => m.UpdateDate).HasColumnName("CmmGoal_UpdateDate");
    
            this.Map(c =>
            {
                c.ToTable("CommissionGoal");
                c.MapInheritedProperties();
            })
            .Map<CommissionGoalByMonth>(c =>
            {
                c.ToTable("CommissionGoalByMonth");
            })
            .Map<CommissionGoalBySales>(c =>
            {
                c.ToTable("CommissionGoalBySales");
            });
        }
    }
    
    public class Context : DbContext
    {
        public Context(string connectionString)
            : base(connectionString)
        {
            Database.SetInitializer<Context>(null);
        }
    
        public DbSet<CommissionGoal> Commissions { get; set; }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new CommissionGoalByMonthConfig());
            modelBuilder.Configurations.Add(new CommissionGoalBySalesConfig());
            modelBuilder.Configurations.Add(new CommissionGoalConfig());
        }
    }
    
    public abstract class CommissionGoal
    {
        public int Id { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime ExpireDate { get; set; }
        public DateTime InsertDate { get; set; }
        public DateTime UpdateDate { get; set; }
    }
    
    public class CommissionGoalByMonth : CommissionGoal
    {
        public int Min { get; set; }
        public int Max { get; set; }
        public decimal MaxPercent { get; set; }
        public decimal Factor { get; set; }
        public int DayLimit { get; set; }
        public int LimitChange { get; set; }
    }
    
    public class CommissionGoalBySales : CommissionGoal
    {
        public int Amount { get; set; }
        public decimal Factor { get; set; }
    }


    Fernando Henrique Inocêncio Borba Ferreira
    while(alive){ this.WriteCode(); }
    Blog: http://ferhenriquef.com/
    Twitter: @ferhenrique


    quarta-feira, 23 de maio de 2012 15:11
    Moderador
  • Cara...

    Ficou fera d+!!!!!!

    Eu nao estava entendendo muito bem como funciona o mapeamento. Normalmente o pessoal só posta exemplo onde o EF cria a estrutura de banco...

    Valeu mesmo!!!

    segunda-feira, 11 de junho de 2012 19:17