【问题标题】:Entity Framework 6 updates all records with data of the one getting updatedEntity Framework 6 使用正在更新的数据更新所有记录
【发布时间】:2021-12-14 14:06:46
【问题描述】:

我正在尝试在我的应用程序中实现实体框架 6,但在执行记录更新时遇到问题。

如果我在数据库中有 2 条记录,可以说:

Id Name Lastname
1 Jason Momoa
2 Aqua Man

然后,我将 id 为 1 的对象从“Jason”更改为“Water”,并使用具有相同主键的新 Person 对象调用 UpdatePerson 函数。

结果将是:

Id Name Lastname
1 Water Momoa
2 Water Momoa

为什么会是这样的结果?我已经在四处寻找解决方案,但找不到任何线索。有人知道我做错了什么吗?

据我了解,我使用的断开连接的数据上下文,可以简单地用主键的知识更新记录。

供参考EF6的页面

我的代码如下所示:

public class Person
{
    private int _id = -1;
    private string _name;
    private string _lastname;

    public int PersonId { get => _id; set => _id = value; }
    [Required]
    [MaxLength(255)]
    public string Name { get => _name; set => _name = value; }
    [Required]
    public string Lastname { get => _lastname; set => _lastname = value; }
}

DbContext:

public partial class Model1 : DbContext
{
    public Model1() : base("name=entity_test") { }

    public DbSet<Person> People { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<Person>().MapToStoredProcedures();
    }     
}

public class PersonModel
{
    public ObservableCollection<Person> GetPeople()
    {
        using (Model1 context = new Model1())
        {
            var list = context.People.AsNoTracking().ToList();
            if (list == null)
                return null;
            return new ObservableCollection<Person>(list);
        }
    }

    public void AddPerson(Person person)
    {
        using (Model1 context = new Model1())
        {
            context.People.Add(person);
            context.SaveChanges();
        }
    }

    public void UpdatePerson(Person person)
    {
        using (Model1 context = new Model1())
        {
            context.Entry(person).State = EntityState.Modified;
            context.SaveChanges();
        }
    }
}

编辑

表格没有很好地显示。

编辑 2

这里是剩下的代码和context.Database.Log = s =&gt; Console.WriteLine(s);的输出

输出:

`Person_Update`


-- PersonId: '1' (Type = Int32, IsNullable = false)

-- Name: 'Water' (Type = String, IsNullable = false, Size = 5)

-- Lastname: 'Momoa' (Type = String, IsNullable = false, Size = 5)

-- Executing at 29.10.2021 16:46:05 +02:00

-- Completed in 198 ms with result: 2

代码:

public class NotifyBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected bool SetProperty<T>(ref T field, T newValue, [CallerMemberName] string propertyName = null)
    {
        if (!EqualityComparer<T>.Default.Equals(field, newValue))
        {
            field = newValue;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            return true;
        }
        return false;
    }
}
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new ViewModel();

    }
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        PersonModel model = new PersonModel();

        if (DataContext is ViewModel vm)
        {
            vm.AddModifyPerson();
        }
    }
}

public class ViewModel : NotifyBase
{
    public ViewModel()
    {
        MiniProfilerEF6.Initialize();
        model = new PersonModel();

        using (var db = new Model1())
        {
            // create if not exists
            if (db.Database.CreateIfNotExists())
            {
                Console.WriteLine();
            }
            People = model.GetPeople();
        }
    }

    private PersonModel model;

    private ObservableCollection<Person> people = new ObservableCollection<Person>();
    private Person currentPerson = new Person();

    public ObservableCollection<Person> People { get => people; set => SetProperty(ref people, value); }
    public Person CurrentPerson { get => currentPerson; set => SetProperty(ref currentPerson, value); }

    public void AddModifyPerson()
    {
        if (CurrentPerson.PersonId == -1)
        {
            model.AddPerson(CurrentPerson);
        }
        else
        {
            model.UpdatePerson(
                new Person()
                {
                    PersonId = CurrentPerson.PersonId,
                    Lastname = CurrentPerson.Lastname,
                    Name = CurrentPerson.Name,
                });
        }
        People = model.GetPeople();
    }
}

编辑 3

来自 miniprofiler 的代码

    public void UpdatePerson(Person person)
    {
        var profiler = MiniProfiler.StartNew("My Profiler");
        using (MiniProfiler.Current.Step("Update_Sql"))
        {
            using (Model1 context = new Model1())
            {
                context.Entry(person).State = EntityState.Modified;
                context.SaveChanges();
            }
        }

        Console.WriteLine(MiniProfiler.Current.RenderPlainText());
    }

编辑 4

mysql.general_log 更新调用的输出

command_type argument
Init DB entity_test
Query SET TRANSACTION ISOLATION LEVEL REPEATABLE READ
Query BEGIN
Query CALL Person_Update(1, 'Jason1', 'Momoa')
Query COMMIT

看起来更新过程没有执行 where 子句。如果我在工作台中运行call Person_Update_Custom(1, 'test','tes'),所有行都会更新。这是存储过程:

    CREATE DEFINER=`root`@`localhost` PROCEDURE `Person_Update`(IN PersonId int,IN Name varchar(255) ,IN Lastname longtext)
    BEGIN 
    UPDATE `People` SET `Name`=Name, `Lastname`=Lastname WHERE `PersonId` = PersonId;
     END

【问题讨论】:

  • 您好,您能否提供一些代码来展示您如何创建新元素并执行更新操作?根据您的描述,您似乎正在执行没有 ID 的更新,您还可以尝试使用 miniprofiler.com 等工具来检查发送到数据库的 SQL 查询,这可能会让您深入了解问题跨度>
  • @ArmandoBracho 我已经添加了其余的代码。在我弄清楚如何使用 miniprofile 之后,我也会添加它。同时,我可以使用命令context.Database.Log = s =&gt; Console.WriteLine(s); 检查 sql 查询,因为结果是 2 条记录被更改。 (有问题的细节编辑2)
  • @ArmandoBracho miniprofiler 的输出是My Profiler 0ms &gt;&gt; Update_Sql 305,5ms (sql = 41,8ms in 9 cmds)(Edit3 中的代码)
  • 错误似乎在存储过程中。
  • 我会考虑在数据库上设置一个 SQL 分析器来捕获正在发送的实际 SQL。我看不出任何明显的东西,但我对在 People 和 CurrentPerson 之间跟踪的对 Person 的引用数量非常谨慎。任何从客户端更新人员的调用都应包含 PersonId。通常对于更新,我会从数据库中获取人,断言行版本没有改变(自从这个客户端读取它之后的另一个更新)然后复制允许更新的字段。使用分离实体允许客户端修改实体上的任何内容。

标签: c# entity-framework entity-framework-6


【解决方案1】:

因此,在 miniprofiler(感谢 Armando Bracho)和 mysql 日志(感谢 Steve Py)中看到始终只有一个 sql 查询并且 Gert Arnold 指出该过程可能失败后,我专注于该过程。

看起来,该过程将PersonId 列名与CREATE PROCEDURE-header 中定义的PersonId 变量混合在一起。

所以我添加了一些代码来手动定义更新过程参数。

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);
    modelBuilder.Entity<Person>().MapToStoredProcedures(p => p.Update(pp => pp.HasName("Person_Update").Parameter(pm => pm.Name, "db_Name").Parameter(pm => pm.Lastname, "db_Lastname").Parameter(pm => pm.PersonId, "db_PersonId")));
}      

将存储过程更改为:

CREATE DEFINER=`root`@`localhost` PROCEDURE `Person_Update`(IN db_PersonId int,IN db_Name varchar(255) ,IN db_Lastname longtext)
BEGIN 
UPDATE `People` SET `Name`=db_Name, `Lastname`=db_Lastname WHERE `PersonId` = db_PersonId;
 END

这终于奏效了!受影响的一行。

编辑

MapToStoredProcedures改成

modelBuilder.Entity<Person>().MapToStoredProcedures(p => p.Update(pp => pp.HasName("Person_Update").Parameter(pm => pm.PersonId, "db_PersonId")));

这种方式不需要为所有属性定义它。

【讨论】:

    猜你喜欢
    • 2014-11-11
    • 1970-01-01
    • 2013-02-26
    • 1970-01-01
    • 2018-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多