在 SET 子句 > 或 INSERT 的列列表中多次指定列名 'userid'
The column name 'userid' is specified more than once in the SET clause > or column list of an INSERT
我最近多次遇到这个错误,现在又出现了。早些时候,我最终重写了几乎所有代码。现在我更想知道为什么会这样。
这是错误:
The column name 'userid' is specified more than once in the SET clause
or column list of an INSERT. A column cannot be assigned more than one
value in the same clause. Modify the clause to make sure that a column
is updated only once. If this statement updates or inserts columns
into a view, column aliasing can conceal the duplication in your code.
这是我的控制器:
[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);
}
这条错误信息是什么意思?为什么我什至不声明用户标识就得到它?
当我遇到 google 这个问题时,他们很少使用 Linq/lambda。
编辑:
因为我在class里找到了解决办法,所以我把原来的class也贴在这里:
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; }
}
我找到了解决方案。
问题是一个相关的实体,它也连接了一个用户标识。所以这是两个不在同一个 table 中的用户标识,但是 ef 找到了两个。
注释掉相关的 table 就解决了。
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; }
}
我最近多次遇到这个错误,现在又出现了。早些时候,我最终重写了几乎所有代码。现在我更想知道为什么会这样。 这是错误:
The column name 'userid' is specified more than once in the SET clause or column list of an INSERT. A column cannot be assigned more than one value in the same clause. Modify the clause to make sure that a column is updated only once. If this statement updates or inserts columns into a view, column aliasing can conceal the duplication in your code.
这是我的控制器:
[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);
}
这条错误信息是什么意思?为什么我什至不声明用户标识就得到它?
当我遇到 google 这个问题时,他们很少使用 Linq/lambda。
编辑: 因为我在class里找到了解决办法,所以我把原来的class也贴在这里:
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; }
}
我找到了解决方案。 问题是一个相关的实体,它也连接了一个用户标识。所以这是两个不在同一个 table 中的用户标识,但是 ef 找到了两个。 注释掉相关的 table 就解决了。
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; }
}