Jeevan Chathuranga
Jeevan Chathuranga

Reputation: 133

Can't insert record.the given error "Table "tbCustomer" does not have the identity property. Cannot perform SET operation"

I do not have identity column on table.I dont need identity for the coulmn named as CountryID. I pass CountryID value from client request for the CountryId column as primary key. once I executed stored procedure I am given the mentioned error as "Table does not have the identity property. Cannot perform SET operation". How can I insert record without getting this error ?

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int CountryID { get; set; }

    public string CountryName { get; set; }

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int LanguageId { get; set; }

enter image description here

   var result =  await tDBContext.Query<SpResponseMessage>().FromSql("exec sp_synchCountry " +
                 "@p0, @p1, @p2",
                  objCountry.CountryID,
                  objCountry.CountryName
                  objCountry.LanguageId).SingleOrDefaultAsync();

Upvotes: 0

Views: 1918

Answers (1)

Xueli Chen
Xueli Chen

Reputation: 12715

If you want to configure multiple properties to be the key of an entity (known as a composite key). Composite keys can only be configured using the Fluent API - conventions will never setup a composite key and you can not use Data Annotations to configure one.

 public class MyDbContext:DbContext
{
    public MyDbContext(DbContextOptions<MyDbContext> options):base(options)
    { }

    public DbSet<Country> tbCountry { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Country>()
            .HasKey(c => new { c.CountryID, c.LanguageId });
    }
}

Reference : https://learn.microsoft.com/en-us/ef/core/modeling/keys#fluent-api

Upvotes: 1

Related Questions