[英]How to update record using Entity Framework 6 MySQL
I am looking for the correct way to update a record using Entity Framework 6 MYSQL. 我正在寻找使用Entity Framework 6 MYSQL更新记录的正确方法。
Below is what I'm using right now. 以下是我现在正在使用的内容。
public void Update(User user)
{
using (var ctx = new DataSystemDbContext())
{
ctx.Users.Attach(user);
ctx.Entry(user).State = EntityState.Modified;
ctx.SaveChanges();
}
}
And on my unit test. 然后在我的单元测试中。
[TestMethod]
public async Task User_Update()
{
var userService = new UserService();
var user = userService.GetById(1);
user.FullName = "Test Fullname";
userService.Update(user);
var updatedUser = userService.GetById(1);
Assert.AreEqual(user.FullName, updatedUser.FullName);
}
When it executes all my user records are updated. 当它执行时,我的所有用户记录都会更新。
This is also related to my another post. 这也与我的另一篇文章有关。 But on my another post it's using a live context, unlike this one.
但是在我的另一篇文章中,它使用的是实时上下文,这与此不同。
MySQL Entity Framework 6 Update affecting all rows Code First MySQL Entity Framework 6更新影响所有行代码优先
I fixed this issue by commenting out or not using the generated procedure for CRUD. 我通过注释掉或不使用CRUD的生成过程来解决此问题。
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// Mapp all entities in a single line.
//modelBuilder.Types().Configure(t => t.MapToStoredProcedures());
}
Generated SQL 生成的SQL
UPDATE `Users` SET `Email`=@gp1 WHERE `Id` = 6
-- @gp1: 'myemail@gmail.com' (Type = String, IsNullable = false, Size = 17)
-- Executing asynchronously at 25/06/2018 8:30:21 AM +08:00
-- Completed in 2 ms with result: 1
I'm not sure if the MySQL Provider has a bug or not. 我不确定MySQL Provider是否存在错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.