我正在尝试在表中插入一行,但这只是在更新表。如何使用Entity Framework在表格中插入新行?
public partial class Transformer_GSM
{
public int id { get; set; }
public double KW { get; set; }
}
表格列
[id] [int] IDENTITY(1,1) PRIMARY KEY,
[KW] [float] NOT NULL
表名:Transformer_GSM
在上下文中-
public DbSet<Transformer_GSM> Transformer_GSM { get; set; }
代码:
public IHttpActionResult GSM(double KW)
{
try
{
using (smartpondEntities DB = new smartpondEntities())
{
DATA.Transformer_GSM t = new Transformer_GSM();
t.KW = KW;
DB.Transformer_GSM.Add(t);
DB.SaveChanges();
}
var response = new
{
Success = true,
Message = "Transformer data saved",
};
return Ok(response);
}
答案 0 :(得分:0)
我的猜测是,由于PK是一个标识列,您需要告诉EF,否则它将认为您正在更新ID = 0的记录。
public partial class Transformer_GSM
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id { get; set; }
public double KW { get; set; }
}