2016-03-02 61 views
-1

使用EF 6.1.3和EF生成重複列,並且在使用update-database -Script命令時不生成某些表。有2個表格有這樣的奇怪和重複的列。EF生成重複列

CREATE TABLE [dbo].[OrcamentoInsumo] (
    [OrcamentoId] [uniqueidentifier] NOT NULL, 
    [CRId] [uniqueidentifier] NOT NULL, 
    [CodigoTron] [varchar](150) NOT NULL, 
    [InsumoId] [uniqueidentifier] NOT NULL, 
    [FamiliaId] [uniqueidentifier] NOT NULL, 
    [Quantidade] [int] NOT NULL, 
    [ValorUnitario] [decimal](18, 5) NOT NULL, 
    [ValorTotal] [decimal](18, 5) NOT NULL, 
    [IsIAC] [bit] NOT NULL, 
    [IsINOC] [bit] NOT NULL, 
    [AditivoContratoId] [uniqueidentifier], 
    [DataCadastro] [datetime], 
    [Observacao] [varchar](150), 
    [UsuarioId] [varchar](150), 
    [DataCadastro1] [datetime], 
    [Observacao1] [varchar](150), 
    [UsuarioId1] [varchar](150), 
    [Discriminator] [nvarchar](128) NOT NULL, 
    [Insumo_InsumoId] [uniqueidentifier], 
    [Usuario_Id] [varchar](128), 
    [Insumo_InsumoId1] [uniqueidentifier], 
    [Familia_FamiliaId] [uniqueidentifier], 
    [Familia_FamiliaId1] [uniqueidentifier], 
    [CR_CRId] [uniqueidentifier], 
    [CR_CRId1] [uniqueidentifier], 
    CONSTRAINT [PK_dbo.OrcamentoInsumo] PRIMARY KEY ([OrcamentoId]) 
) 

這裏是型號:

public class OrcamentoInsumo 
    { 
     public Guid OrcamentoId { get; set; } 
     public Guid CRId { get; set; } 
     public virtual CR CR { get; set; } 
     public String CodigoTron { get; set; } 
     public Guid InsumoId { get; set; } 
     public virtual Insumo Insumo { get; set; } 
     public Guid FamiliaId { get; set; } 
     public virtual Familia Familia { get; set; } 
     public int Quantidade { get; set; } 
     public decimal ValorUnitario { get; set; } 
     public decimal ValorTotal { get; set; } 
     public virtual bool IsIAC { get; protected set; } 
     public virtual bool IsINOC { get; protected set; } 
    } 

而且我在我的背景下面幾行:

modelBuilder.Entity<InsumoPedido>().Map(m => 
      { 
       m.MapInheritedProperties(); 
       m.ToTable("InsumoPedido"); 
      }); 
public DbSet<OrcamentoInsumo> OrcamentoInsumo { get; set; } 

這裏是Fluet API代碼:

public OrcamentoInsumoConfig() 
     { 
      HasKey(o => o.OrcamentoId); 

      HasRequired(o => o.CR) 
       .WithMany(o => o.OrcamentoInsumo) 
       .HasForeignKey(o => o.CRId); 

      HasRequired(o => o.Familia) 
       .WithMany(o => o.OrcamentoInsumo) 
       .HasForeignKey(o => o.FamiliaId); 

      HasRequired(o => o.Insumo) 
       .WithMany(o => o.OrcamentoInsumo) 
       .HasForeignKey(o => o.InsumoId); 

      Property(o => o.Quantidade) 
       .IsRequired(); 

      Property(r => r.IsIAC) 
       .IsRequired(); 

      Property(r => r.IsINOC) 
       .IsRequired(); 

      Property(o => o.CodigoTron) 
       .IsRequired(); 

      Property(o => o.ValorUnitario) 
       .IsRequired(); 

      Property(o => o.ValorTotal) 
       .IsRequired(); 

Familia Fluent API代碼:

public FamiliaConfig() 
     { 
      HasKey(f => f.FamiliaId); 

      Property(f => f.CodigoTron) 
       .IsRequired(); 

      HasRequired(f => f.TD) 
       .WithMany(f => f.Familias) 
       .HasForeignKey(f => f.TDId); 

      Property(f => f.Descricao) 
       .IsRequired() 
       .HasMaxLength(null); 
     } 

這裏是我的IAC類,它繼承與OrcamentoInsumo

public class IAC : OrcamentoInsumo 
    { 
     public override bool IsIAC 
     { 
      get 
      { 
       return base.IsIAC; 
      } 

      protected set 
      { 
       base.IsIAC = true; 
      } 
     } 
     public Guid AditivoContratoId { get; set; } 
     public virtual AditivoContrato AditivoContrato { get; set; } 
     public DateTime DataCadastro { get; set; } 
     public String Observacao { get; set; } 
     public String UsuarioId { get; set; } 
     public virtual Usuario Usuario { get; set; } 

IAC映射:

public IACConfig() 
     { 
      HasKey(i => i.OrcamentoId); 

      HasRequired(i => i.CR) 
       .WithMany(i => i.IAC) 
       .HasForeignKey(i => i.CRId); 

      HasRequired(i => i.Familia) 
       .WithMany(i => i.IAC) 
       .HasForeignKey(i => i.FamiliaId); 

      HasRequired(i => i.Insumo) 
       .WithMany(i => i.IAC) 
       .HasForeignKey(i => i.InsumoId); 

      HasRequired(i => i.Usuario) 
       .WithMany(i => i.IAC) 
       .HasForeignKey(i => i.UsuarioId); 

      HasRequired(i => i.AditivoContrato) 
       .WithMany(i => i.IAC) 
       .HasForeignKey(i => i.AditivoContratoId); 

      Property(i => i.DataCadastro) 
       .IsRequired(); 

      Property(i => i.ValorTotal) 
       .IsRequired(); 

      Property(i => i.Observacao) 
       .HasMaxLength(null); 
     } 

INOC類,它也有繼承與OrcamentoInsumo

public class INOC : OrcamentoInsumo 
    { 
     public override bool IsINOC 
     { 
      get 
      { 
       return IsINOC; 
      } 

      protected set 
      { 
       IsINOC = true; 
      } 
     } 
     public DateTime DataCadastro { get; set; } 
     public String Observacao { get; set; } 
     public String UsuarioId { get; set; } 
     public virtual Usuario Usuario { get; set; } 

INOC映射

public INOCConfig() 
     { 
      HasKey(i => i.OrcamentoId); 

      HasRequired(i => i.CR) 
       .WithMany(i => i.INOC) 
       .HasForeignKey(i => i.CRId); 

      HasRequired(i => i.Familia) 
       .WithMany(i => i.INOCs) 
       .HasForeignKey(i => i.FamiliaId); 

      HasRequired(i => i.Insumo) 
       .WithMany(i => i.INOC) 
       .HasForeignKey(i => i.InsumoId); 

      HasRequired(i => i.Usuario) 
       .WithMany(i => i.INOC) 
       .HasForeignKey(i => i.UsuarioId); 

      Property(i => i.DataCadastro) 
       .IsRequired(); 

      Property(i => i.ValorUnitario) 
       .IsRequired(); 

      Property(i => i.ValorTotal) 
       .IsRequired(); 

      Property(i => i.CodigoTron) 
       .IsRequired(); 

      Property(i => i.Quantidade) 
       .IsRequired(); 

      Property(i => i.Observacao) 
       .HasMaxLength(null); 
     } 
+0

你也在你的相關實體中有反向映射嗎?例如Familia – KevDev

+0

我認爲不是......我用我的familia fluent API代碼更新我的問題。 – user3670112

+0

我試圖使用您的輸入重現問題,但沒有成功 - 所有列都正確生成,沒有重複。你有繼承你的'OrcamentoInsumo'的類嗎?看起來像你提供的代碼之外的東西是這樣做的。 –

回答

1

ToTable("tablename");添加到您的配置應該可以解決您的問題。例如,它會尋找這樣的層次結構:

public class OrcamentoInsumoConfig : EntityTypeConfiguration<OrcamentoInsumo> 
{ 
    public OrcamentoInsumoConfig() 
    { 
     ToTable("OrcamentoInsumo"); 

     HasKey(o => o.OrcamentoId); 

     ... 
    } 
} 

public class INOCConfig : EntityTypeConfiguration<INOC> 
{ 
    public INOCConfig() 
    { 
     ToTable("INOC"); 

     HasKey(i => i.OrcamentoId); 

     ... 
    } 
} 

public class IACConfig : EntityTypeConfiguration<IAC> 
{ 
    public IACConfig() 
    { 
     ToTable("IACC"); 

     HasKey(i => i.OrcamentoId); 

     ... 
    } 
} 

可以查看每個具體類表的詳細信息,實體框架在這裏:

http://weblogs.asp.net/manavi/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-3-table-per-concrete-type-tpc-and-choosing-strategy-guidelines

https://msdn.microsoft.com/en-us/data/jj591617.aspx#2.6

+0

謝謝,它解決了這個問題,只需要考慮一下,我必須在執行update-database命令之前重建我的解決方案。當我沒有重建執行命令時,它沒有任何作用。 – user3670112

1

在模型上嘗試使用數據標註爲創建異物 試試這個:

public class OrcamentoInsumo 
{ 
    public Guid OrcamentoId { get; set; } 

    [ForeignKey("CR")] 
    public Guid CRId { get; set; } 
    public virtual CR CR { get; set; } 

    public String CodigoTron { get; set; } 

    [ForeignKey("Insumo")] 
    public Guid InsumoId { get; set; } 
    public virtual Insumo Insumo { get; set; } 

    [ForeignKey("Familia")] 
    public Guid FamiliaId { get; set; } 
    public virtual Familia Familia { get; set; } 

    public int Quantidade { get; set; } 

    public decimal ValorUnitario { get; set; } 

    public decimal ValorTotal { get; set; } 

    public virtual bool IsIAC { get; protected set; } 

    public virtual bool IsINOC { get; protected set; } 
} 

我不建議直接用流利的API添加主鍵和外鍵,我建議只對更嚴格的設置。

這可以用數據註釋來總結,由於您的表的「業務規則」將只保留在一個類中,因此執行維護要容易得多。

+0

Paulo我嘗試了你的方法,但結果仍然一樣。 – user3670112

1

我假設額外的列,因爲你沒有用流利的API設置你的外鍵關係約束故產生額外的列

在你流利的API,做這樣的事情。

HasRequired(t => t.Familia) 
       .WithMany() // Cant see your Familia class 
       .HasForeignKey(d => d.FamiliaId); 

這需要爲您的所有外鍵關係完成。

+0

我更新了我的問題,看起來我就像你發佈的一樣,並且我在InsumoOrcamento中的所有FK都在Fluent API類中。 – user3670112