如何解决在SET子句>或INSERT的列列表中多次指定了列名“ userid”
我最近多次遇到此错误,现在又出现了。早些时候,我最终重写了几乎所有代码。现在,我对了解为什么会发生这种情况更感兴趣。 这是错误:
在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时也得到它?
当我用google搜索这个问题时,很少他们使用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; }
}
解决方法
我找到了解决方案。 问题是相关实体,该实体也已连接了用户标识。因此,这是两个不在同一表中的用户ID,但是ef找到了两个。 注释掉相关表即可解决此问题。
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; }
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。