-1

我的表上没有身份列。我不需要名为 CountryID 的列的身份。我将来自客户端请求的 CountryID 值传递给 CountryId 列作为主键。一旦我执行存储过程,我就会收到提到的错误,因为“表没有身份属性。无法执行 SET 操作”。如何插入记录而不出现此错误?

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

    public string CountryName { get; set; }

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

在此处输入图像描述

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

1 回答 1

1

如果要将多个属性配置为实体的键(称为复合键)。复合键只能使用 Fluent API 进行配置 - 约定永远不会设置复合键,您也不能使用数据注释来配置复合键。

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

参考:https ://docs.microsoft.com/en-us/ef/core/modeling/keys#fluent-api

于 2019-10-08T09:18:32.130 回答