实体框架比较两个数据集

时间:2021-02-10 02:05:40

I have a situation where I need to compare two sets of data from 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. The fields returned by the stored procedure are identical to the fields in the table.

我有一种情况需要比较来自Sql Server的两组数据。一组数据保存在表中(在我的实体框架模型中建模),但另一组是存储过程的结果集。存储过程返回的字段与表中的字段相同。

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 MyTables which I could populate with the result of the stored procedure, and then compare that to the data in the table.

逻辑是我认为db.Set 会创建一个任意空的MyTables集合,我可以用存储过程的结果填充它,然后将其与表格中的数据进行比较。

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.

但是,我似乎误解了这一点,因为检查tableData和procData的内容在第一行if显示两者都包含完全相同的数据(我故意编辑存储过程,因此它不会返回与表),让我相信db.Set

和db.MyTables都引用相同的东西。

How can I achieve this?

我怎样才能做到这一点?

1 个解决方案

#1


1  

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; }

public DbSet MyTables {get;组; }

Calling db.Set<MyTable>() will give you a set equivalent to the property defined on the context.

调用db.Set ()将为您提供与上下文中定义的属性等效的集合。

If you are simply filtering in the stored procedure then tableData contains all records (so does 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". 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).

如果您只是在存储过程中进行过滤,那么tableData包含所有记录(procData也是如此)。当您尝试添加更多记录(表面上包含相同的记录)时,EF将尝试添加状态为“已添加”的记录。问题是,您的比较不是测试状态的差异,EF可能不会考虑添加它们(如果您只是配置了主键,EF可能会确定记录已存在且状态更改是不必要的)。

#1


1  

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; }

public DbSet MyTables {get;组; }

Calling db.Set<MyTable>() will give you a set equivalent to the property defined on the context.

调用db.Set ()将为您提供与上下文中定义的属性等效的集合。

If you are simply filtering in the stored procedure then tableData contains all records (so does 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". 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).

如果您只是在存储过程中进行过滤,那么tableData包含所有记录(procData也是如此)。当您尝试添加更多记录(表面上包含相同的记录)时,EF将尝试添加状态为“已添加”的记录。问题是,您的比较不是测试状态的差异,EF可能不会考虑添加它们(如果您只是配置了主键,EF可能会确定记录已存在且状态更改是不必要的)。