IDENTITY_INSERT is set to OFF - Entity Framework Core

Asked

Viewed 851 times

0

I’m working on an application with + and with mapping and configuration via . When I go into the bank I get the error that the IDENTITY_INSERT is defined as OFF.

Follow the Image: inserir a descrição da imagem aqui

I have the entities (which are related):

  • Material;
  • Packing;
  • Material Group;
  • Unidademedida;
  • Registro Anvisa;

Code of Entities

public class Material
{
    public Material()
    {
        Registros = new List<RegistroANVISA>();
    }

    public int MaterialId { get; set; }
    public string Descricao { get; set; }
    public int NumeroMaxProcessamento { get; set; }
    public string Imagem { get; set; }

    public bool Ativo { get; set; }

    [JsonIgnore]
    public int GrupoMaterialId { get; set; }
    [JsonIgnore]
    public virtual GrupoMaterial GrupoMaterial { get; set; }
    [JsonIgnore]
    public int UnidadeMedidaId { get; set; }
    [JsonIgnore]
    public virtual UnidadeMedida UnidadeMedida { get; set; }
    [JsonIgnore]
    public int EmbalagemId { get; set; }
    [JsonIgnore]
    public virtual Embalagem Embalagem { get; set; }
    [JsonIgnore]
    public virtual IEnumerable<RegistroANVISA> Registros { get; set; }
}

public class Embalagem
{
    public Embalagem()
    {
        Materiais = new List<Material>();
    }

    public int EmbalagemId { get; set; }
    public string Descricao { get; set; }
    public bool Ativo { get; set; }

    public virtual ICollection<Material> Materiais { get; set; }
}

public class GrupoMaterial
{
    public GrupoMaterial()
    {
        Materiais = new List<Material>();
    }

    public int GrupoMaterialId { get; set; }
    public string Descricao { get; set; }
    public string Observacao { get; set; }
    public bool Ativo { get; set; }

    public virtual ICollection<Material> Materiais { get; set; }
}

public class UnidadeMedida
{
    public UnidadeMedida()
    {
        Materiais = new List<Material>();
    }

    public int UnidadeMedidaId { get; set; }
    public string Descricao { get; set; }
    public string Sigla { get; set; }
    public bool Ativo { get; set; }

    public virtual ICollection<Material> Materiais { get; set; }
}

public class RegistroANVISA
{
    public RegistroANVISA()
    {

    }

    public int RegistroId { get; set; }
    public int NumeroRegistro { get; set; }
    public string Marca { get; set; }
    public bool Ativo { get; set; }

    public int MaterialId { get; set; }
    public virtual Material Material { get; set; }
}

Setting up the Fluent Api

public void Configure(EntityTypeBuilder<Material> builder)
{
    builder.ToTable("Metrial");

    builder.Property(m => m.Descricao)
        .HasColumnType("varchar(150)")
        .IsRequired();

    builder.HasKey(m => m.MaterialId);

    builder.Property(m => m.EmbalagemId)
        .ValueGeneratedOnAddOrUpdate();

    builder.Property(m => m.GrupoMaterialId)
        .ValueGeneratedOnAddOrUpdate();

    builder.Property(m => m.UnidadeMedidaId)
        .ValueGeneratedOnAddOrUpdate();


    //Define relacionamento um-para-muito 
    builder.HasOne(m => m.GrupoMaterial)
        .WithMany(m => m.Materiais)
       // .HasForeignKey(m=> m.GrupoMaterial.GrupoMaterialId)
        .OnDelete(DeleteBehavior.Restrict);

    //Define relacionamento um-para-muito 
    builder.HasOne(m => m.Embalagem)
        .WithMany(m => m.Materiais)
        .OnDelete(DeleteBehavior.Restrict);

    //Define relacionamento um-para-muito 
    builder.HasOne(m => m.UnidadeMedida)
        .WithMany(m => m.Materiais)
        .OnDelete(DeleteBehavior.Restrict);

    //Define relacionamento um-pra-muitos (um material com muitos registros)
    builder.HasMany(m => m.Registros)
        .WithOne(m => m.Material)
        .OnDelete(DeleteBehavior.Restrict);
}

What am I doing wrong?

  • ueh, but you want to insert the PK in hand?

  • @Barbetta - No, as far as I know if the objects are complete and the relationships are correct, the insertion of PK and the rest is in charge of Entity-framework-core, correct? If so, I’m trying to find what I’m doing wrong so it doesn’t happen...

  • More or less. But the error there is saying that you tried to enter a primary key for the object UnidadeMedida. The default is OFF even, this indicates that Sqlserver will manage the PK’s, see this article: http://www.codigomaster.com.br/banco-dados/set-identity_insert-sql-server-on-off

  • Try debugging and see if before giving the SaveChanges() the object has value in the primary key

  • @Barbetta - I checked and before Savechanges() the material related objects have the primary keys (once they are selected by a dropdownlist) and arrive at the Repository with their respective primary keys. The only thing it doesn’t have is the material class being saved in the bank.

  • When you are adding items to the object Material you do how? makes 1 select in the bank and fills the property Material.UnidadeMedida besides the Material.UnidadeMedidaId?. If yes there is the problem, do not need to do so. in the object Material the Material.UnidadeMedida shall be null and shall have value only in Material.UnidadeMedidaId if he doesn’t try to make a Insert in "cascade"

  • @Barbetta - Question: I have my Material object and related to it I have my Material Group object. Ex: Material {Description: "xxx", Active: True, Groupmaterial: new{Id: 2,Description: "xxxx", Active: True}} Entity-framework-core will insert the material and PK or insert the material and the material group?

  • 1
  • 1

    @Barbetta your guidance worked out! Problem Solved!!! Thank you so much!

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.