I am having the .Net Core Entity Framework Code First Web API solution. I am trying to create new Model using Code First approach. I have use “HasKey” option but is creating Cluster Index and Unique Constraint as well. Cluster Index that can be understandable however, it is not getting clear why it is creating Unique Constraint. I do not want to be create additional unique constraint on it. Below is my code for the reference.
I have tried below code
“[Table(“VendorBrandsPromotion”)]
public class VendorBrandPromoTable
{
public Guid Id { get; set; }
public Guid VendorId { get; set; }
public string Brand { get; set; }
public string Location { get; set; }
public string CustomerGroup { get; set; }
public bool IsEnableDateValidation { get; set; }
public DateTime? StartDate { get; set; }
public DateTime? EndDate { get; set; }
public string HeaderText { get; set; }
public string BodyText { get; set; }
public string PromotionText { get; set; }
public bool IsActive { get; set; }
public int CreatedBy { get; set; }
public DateTime CreatedDate { get; set; }
public int? UpdatedBy { get; set; }
public DateTime? UpdatedDate { get; set; }
public virtual ICollection<BrandsPromotionProductsTable> BrandsPromotionProducts { get; set; }
public virtual ICollection<BrandsPromotionImagesTable> BrandsPromotionImages { get; set; }
//Config
public class DbConfiguration : IEntityTypeConfiguration<VendorBrandPromoTable>
{
public void Configure(EntityTypeBuilder<VendorBrandPromoTable> builder)
{
builder.HasKey(v => new { v.Id, v.VendorId });
builder.Property(v => v.Brand).HasMaxLength(500).IsRequired();
builder.Property(v => v.Location).HasMaxLength(500);
builder.Property(v => v.CustomerGroup).HasMaxLength(500);
builder.Property(v => v.HeaderText).HasMaxLength(500);
builder.Property(v => v.BodyText).HasMaxLength(1000);
builder.Property(v => v.PromotionText).HasMaxLength(80);
builder.HasMany(v => v.BrandsPromotionProducts).WithOne(v => v.VendorBrandPromo).HasForeignKey(v => new { v.VendorPromotionId }).HasPrincipalKey(v => new { v.Id });
builder.HasMany(v => v.BrandsPromotionImages).WithOne(v => v.VendorBrandPromo).HasForeignKey(v => new { v.VendorPromotionId }).HasPrincipalKey(v => new { v.Id });
}
}
}`
Kindly help me what and where I am doing wrong here.
Pooja Pandey is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.