【发布时间】:2021-01-15 00:52:46
【问题描述】:
我最近多次遇到此错误,现在又出现了。早些时候,我最终重写了几乎所有的代码。现在我更感兴趣的是知道为什么会发生这种情况。 这是错误:
在 SET 子句中多次指定了列名 'userid' 或 INSERT 的列列表。一列不能分配多个 同一条款中的价值。修改子句以确保列 只更新一次。如果此语句更新或插入列 在视图中,列别名可以隐藏代码中的重复。
这是我的控制器:
[HttpPost]
//[Route("api/Ratings/PostRating/")]
public async Task<ActionResult<Ratings>> PostRating([FromBody] JObject RatingInfo )
{
int id = (int)RatingInfo["id"];
int val = (int)RatingInfo["val"];
//Guid userid = Guid.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier));
//Guid _userid = Guid.Parse("27E72CE4-FC2L-4C18-A27E-974CB4F4448D");
if (val > 1 || val < 0)
{
BadRequest();
}
var ratings = await _context.Ratings.FindAsync(id);
if (ratings == null)
{
var newRating = new Ratings();
//Does not exist
newRating.Type = "Books";
newRating.Rating = val;
newRating.Bookid = id;
//newRating.Userid = userid;
_context.Ratings.Add(newRating);
await _context.SaveChangesAsync();
}
else
{
//Does exist
ratings.Active = val;
_context.Entry(ratings).Property("Active").IsModified = true;
await _context.SaveChangesAsync();
}
return Ok();
//await _context.SaveChangesAsync();
//return CreatedAtAction("GetRatings", new { id = ratings.Id }, ratings);
}
这个错误信息是什么意思?为什么我什至没有声明 userid 时会得到它?
当我用谷歌搜索这个问题时,他们很少使用 Linq/lambda。
编辑: 因为我在课堂上找到了解决方案,所以我也将原来的类贴在这里:
public partial class Ratings
{
public int Id { get; set; }
public int? Bookid { get; set; }
public string Type { get; set; }
public int Rating { get; set; }
public Guid Userid { get; set; }
public string Subject { get; set; }
public DateTime Createdon { get; set; }
public DateTime? Modifiedon { get; set; }
public int Active { get; set; }
public virtual Users User { get; set; }
}
【问题讨论】:
-
有
var ratings = _context.Ratings.Where(c => c.Id == id && c.Userid == _userid).FirstOrDefault();给出了同样的错误。 -
这听起来像是在生成错误的 SQL。您使用的是最新的 EF Core 3.1 还是旧版本?
-
3.1.7 在我的包管理器控制台中这样说
标签: linq asp.net-core .net-core lambda webapi