C# EF生成重复的列
当我使用updatedatabase-Script命令时,Im使用ef6.1.3和EF生成重复的列,而不是生成一些表。有两个表具有奇怪的重复列,如下所示C# EF生成重复的列,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,当我使用updatedatabase-Script命令时,Im使用ef6.1.3和EF生成重复的列,而不是生成一些表。有两个表具有奇怪的重复列,如下所示 CREATE TABLE [dbo].[OrcamentoInsumo] ( [OrcamentoId] [uniqueidentifier] NOT NULL, [CRId] [uniqueidentifier] NOT NULL, [CodigoTron] [varchar](150) NOT NULL, [
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])
)
HasRequired(t => t.Familia)
.WithMany() // Cant see your Familia class
.HasForeignKey(d => d.FamiliaId);
模型如下:
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; }
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; }
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; }
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 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; }
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);
}
尝试在模型上使用数据注释来创建外部对象 试试这个:
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; }
}
我不建议直接在fluent api中添加主键和外键,我只建议更严格的设置
这可以用数据注释来概括,因为表的“业务规则”将只保留在一个类中,所以执行维护要容易得多。我假设额外的列是因为您没有使用fluent api设置外键关系约束,所以它会生成额外的列 在fluentapi中,执行以下操作
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])
)
HasRequired(t => t.Familia)
.WithMany() // Cant see your Familia class
.HasForeignKey(d => d.FamiliaId);
这需要为您的所有外键关系执行。添加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);
...
}
}
public类或acmentoinsumoconfig:EntityTypeConfiguration
{
公共或社区设施()
{
ToTable(“OrcamentoInsumo”);
HasKey(o=>o.OrcamentoId);
...
}
}
公共类INOCConfig:EntityTypeConfiguration
{
公共INOCConfig()
{
ToTable(“INOC”);
HasKey(i=>i.OrcamentoId);
...
}
}
公共类IACConfig:EntityTypeConfiguration
{
公共配置()
{
可折叠(“IACC”);
HasKey(i=>i.OrcamentoId);
...
}
}
您可以在此处查看有关实体框架每个具体类的表的更多信息:
我更新了我的问题,看,我是按照你的帖子做的,我在InsumoOrcamento中的所有FK都在Fluent API类中。你的相关实体中也有反向映射吗?我想不是。。。我用familia fluent API代码更新了我的问题。我尝试使用您的输入重现问题,但没有成功-所有列都正确生成,没有重复。您是否有继承您的OrcamentoInsumo
的类?似乎你提供的代码之外的东西可以做到这一点。是的,有两个类继承了Orcamentoinsamo,现在你告诉我,我刚刚意识到ASE 2类并没有在脚本上生成表。我有另一个类也没有生成表,它也有继承,基类在表中也有奇怪的字段,所以我认为这就是问题所在。我如何解决这个问题?@user3670112您能将这两个实体的当前映射添加到您的帖子中吗?如果我看到他们,我可以告诉你你的错误。您还应该考虑这里需要的继承类型。应该是每个实体的表还是每个层次结构的表?Paulo我尝试了您的方法,但结果仍然相同。谢谢您,它解决了问题,仅需考虑一点,我必须在执行更新数据库命令之前重建我的解决方案。当我在没有重建的情况下执行命令时,它没有任何效果。