C# 实体框架锁定数据库

C# 实体框架锁定数据库,c#,entity-framework-6,C#,Entity Framework 6,下面的查询速度很慢,效率也很低。根据用户提供的参数,运行可能需要一分钟。显然,我们可以提高查询的效率,但我们的问题是,当查询运行时,它似乎会锁定数据库 在我们的客户站点上,当它运行时,我们的应用程序将挂在客户站点上的每台pc上,直到它完成。如果我们在ManagementStudio中运行相同的查询,运行大约需要一分钟,但不会影响任何其他pc终端。数据库没有锁定 有人知道为什么EF会发生这种情况吗?(我们正在使用EF6) 编辑:重申一下-我们的问题不是如何改进查询(我们可以做到)…这就是为什么在实

下面的查询速度很慢,效率也很低。根据用户提供的参数,运行可能需要一分钟。显然,我们可以提高查询的效率,但我们的问题是,当查询运行时,它似乎会锁定数据库

在我们的客户站点上,当它运行时,我们的应用程序将挂在客户站点上的每台pc上,直到它完成。如果我们在ManagementStudio中运行相同的查询,运行大约需要一分钟,但不会影响任何其他pc终端。数据库没有锁定

有人知道为什么EF会发生这种情况吗?(我们正在使用EF6)

编辑:重申一下-我们的问题不是如何改进查询(我们可以做到)…这就是为什么在实体框架中运行此查询时会锁定数据库(影响客户端站点上运行的应用程序的每个实例),而在management studio中运行时不会影响任何实例

var details =
                        (from stock in
                             entities.StockTransactions.Where(x => x.Deleted == null 
                                                                   && x.NouTransactionTypeID == NouvemGlobal.TransactionTypeGoodsReceiptId
                                                                   && x.IsBox == true)
                         join attribute in entities.Attributes.Where(x => DbFunctions.TruncateTime(x.GradingDate) >= start &&
                                                             DbFunctions.TruncateTime(x.GradingDate) <= end && x.NouDocStatusID != status) on stock.AttributeID equals attribute.AttributeID
                         join intakeDetail in entities.APGoodsReceiptDetails on stock.MasterTableID equals intakeDetail.APGoodsReceiptDetailID
                         join intake in entities.APGoodsReceipts on intakeDetail.APGoodsReceiptID equals intake.APGoodsReceiptID
                         select new StockDetail
                         {
                             LoadingStockDetail = true,
                             Paid = stock.KillPaymentItems.Any(pay => pay.Deleted == null),
                             StockTransactionID = stock.StockTransactionID,
                             APGoodsReceiptDetailID = stock.MasterTableID,
                             AttributeID = attribute.AttributeID,
                             TransactionWeight = stock.TransactionWeight,
                             Serial = stock.Serial,
                             TransactionDate = stock.TransactionDate,
                             Detained = attribute.Detained,
                             Condemned = attribute.Condemned,
                             Eartag = attribute.Eartag,
                             KillType = attribute.KillType,
                             NouDocStatusID = attribute.NouDocStatusID,
                             Breed = attribute.NouBreed,
                             Grade = attribute.Grade,
                             DOB = attribute.DOB,
                             Identigen = attribute.Identigen,
                             HoldingNumber = attribute.HoldingNumber,
                             CountryOfOrigin = attribute.CountryOfOrigin,
                             KillNumber = attribute.KillNumber,
                             Generic1 = attribute.Generic1,
                             CarcassNumber = attribute.CarcassNumber,
                             CarcassSide = attribute.CarcassSide == 1 ? StockDetail.Side.Side1 : StockDetail.Side.Side2,
                             AgeInMonths = attribute.AgeInMonths,
                             AgeInDays = attribute.AgeInDays,
                             Cleanliness = attribute.NouCleanliness,
                             SequencedDate = attribute.SequencedDate,
                             Category = attribute.NouCategory,
                             Sex = attribute.Sex,
                             GradingDate = attribute.GradingDate,
                             CustomerID = attribute.Customer,
                             FarmAssured = attribute.FarmAssured,
                             Clipped = attribute.Clipped,
                             Casualty = attribute.Casualty,
                             Lame = attribute.Lame,
                             IsFullCarcass = stock.IsBox,
                             NumberOfMoves = attribute.NoOfMoves,
                             PreviousResidency = attribute.PreviousResidency,
                             TotalResidency = attribute.TotalResidency,
                             CurrentResidency = attribute.CurrentResidency,
                             DateOfLastMove = attribute.DateOfLastMove,
                             Imported = attribute.Imported,
                             PaidWeight = attribute.ColdWeight,
                             UTM = attribute.AgeInMonths != null && attribute.AgeInMonths <= ApplicationSettings.GraderLabelUOMAge ? 1 : 0,
                             OTM = attribute.AgeInMonths != null && attribute.AgeInMonths > ApplicationSettings.GraderLabelUOMAge ? 1 : 0,
                             SupplierID = intake.BPMasterID_Supplier
                         })
                            .ToList();
var详细信息=
(来自库存)
entities.StockTransactions.Where(x=>x.Deleted==null
&&x.NouTransactionTypeID==NouvemGlobal.TransactionTypeGoodsReceiptId
&&x.IsBox==true)
在entities.Attributes.Where(x=>DbFunctions.TruncateTime(x.GradingDate)>=start中加入属性&&
DbFunctions.TruncateTime(x.GradingDate)pay.Deleted==null),
StockTransactionID=stock.StockTransactionID,
APGoodsReceiptDetailID=stock.MasterTableID,
AttributeID=attribute.AttributeID,
TransactionWeight=stock.TransactionWeight,
Serial=stock.Serial,
TransactionDate=stock.TransactionDate,
扣留=属性。扣留,
谴责的,谴责的,
Eartag=attribute.Eartag,
KillType=attribute.KillType,
NouDocStatusID=attribute.NouDocStatusID,
繁殖=属性。无繁殖,
等级=属性。等级,
DOB=attribute.DOB,
Identigen=属性。Identigen,
HoldingNumber=属性。HoldingNumber,
CountryOfOrigin=attribute.CountryOfOrigin,
KillNumber=attribute.KillNumber,
Generic1=attribute.Generic1,
carcasnumber=属性。carcasnumber,
carcasside=attribute.carcasside==1?StockDetail.Side.Side1:StockDetail.Side.Side2,
AgeInMonths=attribute.AgeInMonths,
AgeInDays=attribute.AgeInDays,
清洁度=属性。无清洁度,
SequencedDate=attribute.SequencedDate,
类别=属性。类别,
性=属性。性,
GradingDate=attribute.GradingDate,
CustomerID=attribute.Customer,
FarmAssured=attribute.FarmAssured,
Clipped=attribute.Clipped,
伤亡=属性。伤亡,
Lame=属性。Lame,
isfullcarcas=stock.IsBox,
NumberOfMoves=attribute.NoOfMoves,
PreviousResidentity=属性。PreviousResidentity,
TotalResidentity=attribute.TotalResidentity,
CurrentResidentity=attribute.CurrentResidentity,
DateOfLastMove=attribute.DateOfLastMove,
已导入=属性。已导入,
PaidWeight=attribute.ColdWeight,
UTM=attribute.AgeInMonths!=null&&attribute.AgeInMonths应用程序设置.graderLabeloImage?1:0,
SupplierID=摄入量.BPMasterID\u供应商
})
.ToList();

使用SQL profilter查看查询实体框架生成的内容。在Microsoft SQL management studio中运行该查询,查看缺少哪些索引以提高性能。SELECT语句通常不会阻止sql,仅当您使用类似MySQL的语句时。若要查看数据库是否已锁定,请尝试打开连接到数据库的附加Visual Studio,并在执行上层查询时执行一些简单查询。从外观上看,这将导致对许多表进行表扫描(这是否会创建一个
交叉连接
语句?)。关于数据库锁定问题,这段代码在哪里运行?在客户端连接到的服务器上(例如WCF服务或ASP.Net页面?),或者每个客户端是否运行自己的查询?如果这是在服务器上运行的,并且返回了很多行,我可以想象这将锁定服务器超过一分钟。我们关心的不是查询性能(查询已更改,现在运行良好)。我们关心的是,当从EF运行此查询时,db明显被锁定。我们不理解为什么从EF运行的低效select查询会锁定数据库,而通过SSM运行的同一查询不会锁定数据库