使用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);
}
你也在你的相关实体中有反向映射吗?例如Familia – KevDev
我认为不是......我用我的familia fluent API代码更新我的问题。 – user3670112
我试图使用您的输入重现问题,但没有成功 - 所有列都正确生成,没有重复。你有继承你的'OrcamentoInsumo'的类吗?看起来像你提供的代码之外的东西是这样做的。 –