简体   繁体   English

实体框架重复记录

[英]Entity framework repeating records

I am working on an Entity framework project,In which i have created and entity class of a table 我正在一个实体框架项目中,我已经在其中创建了一个表的实体类

and i am returning its records, below is my method of retrieving records 我要返回其记录,下面是我检索记录的方法

 public UserResource GetData(long Id)
        {
            try
            {

                return dataContext.UserResources.Where(r => r.UserID == Id && r.Resource.IsActive == true).FirstOrDefault();


            }
            catch (Exception ex)
            {
              throw ex;
            }
        }

and then on controller i am adding it to List 然后在控制器上我将其添加到列表

List<UserResource> objlst = new List<UserResource>();
for(int i=0;i<100;i++)
{
var data = objResourceRepository.GetData(userIds[i].UserID);
if (data != null)
objlst.Add(data);
}

userIds are coming from a separate Method The problem i am having is that the data is adding twice ie one record is adding two times, what is the problem with this code and what alternative should i use GetData() method? userIds来自单独的方法我遇到的问题是数据要添加两次,即一条记录要添加两次,此代码有什么问题,我应该使用GetData()方法替代什么?

Either you have duplicate records for the same useId in your UserResources table, or you have duplicated userIds in your userIds array. 您在UserResources表中具有相同useId的重复记录,或者在userIds数组中具有重复的userId。

Either way, going to the DB separately for each of 100 records seems a very inefficient way of doing things. 无论哪种方式,对于100条记录中的每条记录分别进入DB似乎都是一种非常低效的处理方式。 It would be better, at least, to pass through the array of use arrays to the GetData function, and use an 'in' clause in there. 至少最好将use数组的数组传递给GetData函数,并在其中使用'in'子句。 Then have your GetData function return an enumeration of UserResources. 然后让您的GetData函数返回UserResources的枚举。

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

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