API allowing duplicate primary key

195 Views Asked by At

I am bulding a Swagger API. I have tried to make my primary key unique but it doesn't seem to work. My DbContext was created by Scaffolding and I have an entity (Product) which primary key is a string.

I don't know why but on my DbContext on ModelCreating it added entity.HasNoKey(). I changed it by hand like so:

 modelBuilder.Entity<CrProduct>(entity =>
            {

                //entity.HasNoKey();
                entity.HasKey(e => e.Reference)
                    .HasName("PRIMARY");

                entity.ToTable("cr_product");

                entity.HasIndex(e => e.IdCategory, "category");

                entity.HasIndex(e => e.IdCompany, "company");

                entity.HasIndex(e => e.Reference, "id_product");

                entity.HasIndex(e => e.Reference, "references");

                entity.Property(e => e.Active)
                    .HasColumnType("tinyint(1) unsigned")
                    .HasColumnName("active")
                    .HasDefaultValueSql("'1'");

                entity.Property(e => e.Description)
                    .IsRequired()
                    .HasMaxLength(255)
                    .HasColumnName("description");

                entity.Property(e => e.IdCategory)
                    .HasColumnType("int(6) unsigned")
                    .HasColumnName("id_category");

                entity.Property(e => e.IdCompany)
                    .HasColumnType("int(6) unsigned")
                    .HasColumnName("id_company");

                entity.Property(e => e.Image)
                    .IsRequired()
                    .HasMaxLength(255)
                    .HasColumnName("image");

                entity.Property(e => e.Name)
                    .IsRequired()
                    .HasMaxLength(100)
                    .HasColumnName("name");

                entity.Property(e => e.Price)
                    .HasColumnType("decimal(15,2)")
                    .HasColumnName("price");

                entity.Property(e => e.Reference)
                    .IsRequired()
                    .HasMaxLength(15)
                    .HasColumnName("reference");

                entity.Property(e => e.Stock)
                    .HasColumnType("int(6) unsigned")
                    .HasColumnName("stock");

                entity.Property(e => e.Warehouse)
                    .HasColumnType("int(6) unsigned")
                    .HasColumnName("warehouse");

                entity.HasOne(d => d.IdCategoryNavigation)
                    .WithMany()
                    .HasForeignKey(d => d.IdCategory)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("cr_product_ibfk_1");

                entity.HasOne(d => d.IdCompanyNavigation)
                    .WithMany()
                    .HasForeignKey(d => d.IdCompany)
                    .OnDelete(DeleteBehavior.ClientSetNull)
                    .HasConstraintName("cr_product_ibfk_2");
            });

When I do a POST request it doesn't matter if the reference field (my primary key) is duplicated as It will add it to my database.

I have tried by adding this annotation to my model but it doesn't work

 [Index(nameof(Reference), IsUnique = true)]
1

There are 1 best solutions below

1
Gabriel Llorico On

if you are using code-first, try adding this to your model

[Key]
public TYPE Reference {get; set;}

if using Model Builder approach

builder.Entity<CrProduct>()
        .HasIndex(u => u.Reference)
        .IsUnique();