EF 生成重复的列

本文关键字:EF | 更新日期: 2023-09-27 18:37:05

我使用 EF

6.1.3 和 EF 生成重复的列,并且在我使用 update-database -Script 命令时不生成一些表。有 2 个表,其中包含像这样的奇怪和重复的列。

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; }

以下是烟道 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; }

伊诺克映射

 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);
        }

EF 生成重复的列

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

我假设额外的列是因为您没有使用流畅的 api 来设置外键关系约束,因此它会生成额外的列

在流畅的 API 中,执行类似操作。

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

需要对所有外键关系执行此操作。

尝试在模型上使用数据注释来创建外来对象试试这个:

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 中添加主键和外键,我只建议进行更严格的设置。

这可以用数据注释来总结,执行维护要容易得多,因为表的"业务规则"将只保留在一个类中。

相关文章:
  • 没有找到相关文章