entity-framework

How can I call local method in Linq to Entities query?

做~自己de王妃 提交于 2020-01-19 12:47:10
问题 I have the following code: public void SalesCount(string customerId) { .. .. return ...; } var resultQuery = dataContext.Customers .Where (c => c.Name == "Alugili") .Where (c => SalesCount(c.CustomerId) < 100); When I execute resultQuery I get a translation to SQL exception. I need to call SalesCount in the Where can I do that is there any workaround for this problem! 回答1: Simply you can't. You can't execute C# code on the SQL server directly, you can use only Expressions and some special

How can I call local method in Linq to Entities query?

半世苍凉 提交于 2020-01-19 12:47:09
问题 I have the following code: public void SalesCount(string customerId) { .. .. return ...; } var resultQuery = dataContext.Customers .Where (c => c.Name == "Alugili") .Where (c => SalesCount(c.CustomerId) < 100); When I execute resultQuery I get a translation to SQL exception. I need to call SalesCount in the Where can I do that is there any workaround for this problem! 回答1: Simply you can't. You can't execute C# code on the SQL server directly, you can use only Expressions and some special

Should the order of LINQ query clauses affect Entity Framework performance?

 ̄綄美尐妖づ 提交于 2020-01-19 05:37:28
问题 I'm using Entity Framework (code first) and finding the order I specify clauses in my LINQ queries is having a huge performance impact, so for example: using (var db = new MyDbContext()) { var mySize = "medium"; var myColour = "vermilion"; var list1 = db.Widgets.Where(x => x.Colour == myColour && x.Size == mySize).ToList(); var list2 = db.Widgets.Where(x => x.Size == mySize && x.Colour == myColour).ToList(); } Where the (rare) colour clause precedes the (common) size clause it's fast, but the

Unit-testing .ToListAsync() using an in-memory

北城余情 提交于 2020-01-19 04:12:25
问题 Below is the kind of test that is failing upon .ShouldNotThrow() due to .ToListAsync() not being supported by in-memory dbsets (I don't have the exact wording handy but you get the picture). In case it's of any importance, I'm trying to mockup the dbset provided by Entity Framework ver. 6.1.3: [TestFixture] public class Tests { private SomeRepository _repository; private Mock<DbSet<SomeEntity>> _mockDbSet; private Mock<IApplicationDbContext> _mockAppDbContext; [OneTimeSetUp] public void

How to do an “in” query in entity framework?

 ̄綄美尐妖づ 提交于 2020-01-18 21:10:47
问题 How can I do a select in linq to entities to select rows with keys from a list? Something like this: var orderKeys = new int[] { 1, 12, 306, 284, 50047}; var orders = (from order in context.Orders where (order.Key in orderKeys) select order).ToList(); Assert.AreEqual(orderKeys.Count, orders.Count); I tried using the Contains method as mentioned in some of the answers but it does not work and throws this exception: LINQ to Entities does not recognize the method 'Boolean Contains[Int32](System

How to do an “in” query in entity framework?

拈花ヽ惹草 提交于 2020-01-18 21:07:18
问题 How can I do a select in linq to entities to select rows with keys from a list? Something like this: var orderKeys = new int[] { 1, 12, 306, 284, 50047}; var orders = (from order in context.Orders where (order.Key in orderKeys) select order).ToList(); Assert.AreEqual(orderKeys.Count, orders.Count); I tried using the Contains method as mentioned in some of the answers but it does not work and throws this exception: LINQ to Entities does not recognize the method 'Boolean Contains[Int32](System

The property 'name' is part of the object's key information and cannot be modified. Entity Framework

試著忘記壹切 提交于 2020-01-18 16:51:09
问题 I am trying to update a record and I get this error message after the context.SaveChanges(); The property 'name' is part of the object's key information and cannot be modified. Here is the code for the update function: if (context.EAT_SourceNames.Any(e => e.name == newSourceName)) { MessageBox.Show("Name already exists in the Database"); } else { var nameToUpdate = context.EAT_SourceNames.SingleOrDefault(e => e.name == sourceName.name); if (nameToUpdate != null) { nameToUpdate.name =

The property 'name' is part of the object's key information and cannot be modified. Entity Framework

守給你的承諾、 提交于 2020-01-18 16:50:27
问题 I am trying to update a record and I get this error message after the context.SaveChanges(); The property 'name' is part of the object's key information and cannot be modified. Here is the code for the update function: if (context.EAT_SourceNames.Any(e => e.name == newSourceName)) { MessageBox.Show("Name already exists in the Database"); } else { var nameToUpdate = context.EAT_SourceNames.SingleOrDefault(e => e.name == sourceName.name); if (nameToUpdate != null) { nameToUpdate.name =

The property 'name' is part of the object's key information and cannot be modified. Entity Framework

北慕城南 提交于 2020-01-18 16:50:02
问题 I am trying to update a record and I get this error message after the context.SaveChanges(); The property 'name' is part of the object's key information and cannot be modified. Here is the code for the update function: if (context.EAT_SourceNames.Any(e => e.name == newSourceName)) { MessageBox.Show("Name already exists in the Database"); } else { var nameToUpdate = context.EAT_SourceNames.SingleOrDefault(e => e.name == sourceName.name); if (nameToUpdate != null) { nameToUpdate.name =

Entity Framework loading child collection with sort order

北战南征 提交于 2020-01-18 07:21:28
问题 I have two tables a parent and a child table. The child table has a column sortorder (a numeric value). Because of the missing support of the EF to persist a IList inclusive the sort order without exposing the sortorder (see: Entity Framework persisting child collection sort order) my child class has also a property SortOrder, so that i can store the children with the sort order. In contrast to the autor of the referenced question i try to load the children always sorted. So if i load a