简体   繁体   English

实体框架比较两个数据集

[英]Entity Framework compare two data sets

I have a situation where I need to compare two sets of data from Sql Server. 我有一种情况需要比较Sql Server的两组数据。 One set of data is held in a table (which is modelled in my Entity Framework model), however the other is the result set of a stored procedure. 一组数据保存在一个表中(在我的Entity Framework模型中建模),但是另一组是存储过程的结果集。 The fields returned by the stored procedure are identical to the fields in the table. 存储过程返回的字段与表中的字段相同。

I had thought the following would work: 我以为以下方法会起作用:

using (var db = new MyDatabaseContext()) {
    DbSet<MyTable> tableData = db.MyTables;
    DbSet<MyTable> procData = db.Set<MyTable>();
    procData.AddRange(db.MyTables.SqlQuery("exec dbo.MyProc").ToList();

    if (tableData.Count != procData.Count) return false;

    foreach (var data in tableData) {
        if (!data.Equals(procData.Find(data.ID))) return false;
    }
}

(side note: The MyTable class has been edited to implement IEquatable and override Equals so it's suitable for comparing at field level) (注意: MyTable类已经过编辑以实现IEquatable并覆盖Equals因此它适合在字段级别进行比较)

The logic being that I believed db.Set<MyTable> would create an arbitrary empty set of MyTable s which I could populate with the result of the stored procedure, and then compare that to the data in the table. 该逻辑是,我相信db.Set<MyTable>将创建一个任意的空集MyTable S的我可以与所存储的过程的结果填充,然后比较,为在表中的数据。

It appears I've misunderstood this, however, as checking the contents of tableData and procData at the first if line shows both contain exactly the same data (I've purposefully editted the stored procedure so it does not return the same data as in the table), leading me to believe that db.Set<Table> and db.MyTables both reference the same thing. 看来不过我误解了这一点,作为检查的内容tableDataprocData在第一if行显示都包含完全相同的数据(我已经有意editted存储过程,因此不会返回相同的数据作为表),使我相信db.Set<Table>db.MyTables都引用相同的内容。

How can I achieve this? 我该如何实现?

db.MyTables has the same definition as what is returned in the line defining procData and initially contains the same objects. db.MyTables具有与定义procData的行中返回的定义相同的定义,并且最初包含相同的对象。

(I assume your DbContext has the following, or something equivalent): (我假设您的DbContext具有以下内容或等效的内容):

public DbSet<MyTable> MyTables { get; set; }

Calling db.Set<MyTable>() will give you a set equivalent to the property defined on the context. 调用db.Set<MyTable>()将为您提供一个与上下文中定义的属性等效的集合。

If you are simply filtering in the stored procedure then tableData contains all records (so does procData ). 如果仅在存储过程中进行过滤,则tableData包含所有记录( procData也是procData )。 When you attempt to add more records (which are ostensibly the same records contained in the set), EF will try and add the records with a state of "Added" . 当您尝试添加更多记录(表面上与集合中包含的记录相同)时, EF将尝试添加状态为“已添加”的记录 The thing is, your comparison isn't testing for the difference in state, and EF might not consider them added (if you configured your primary keys just so, EF might determine that the records already exist and state changes are unnecessary). 事实是,您的比较并没有测试状态差异,并且EF可能不会考虑添加它们(如果正好配置了主键,EF可能会确定记录已经存在,并且不需要更改状态)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM