How to get max value of a column using Entity Framework?
Try this int maxAge = context.Persons.Max(p => p.Age); And make sure you have using System.Linq; at the top of your file
Try this int maxAge = context.Persons.Max(p => p.Age); And make sure you have using System.Linq; at the top of your file
There is opportunity for several improvements (if you are using DbContext): Set: yourContext.Configuration.AutoDetectChangesEnabled = false; yourContext.Configuration.ValidateOnSaveEnabled = false; Do SaveChanges() in packages of 100 inserts… or you can try with packages of 1000 items and see the changes in performance. Since during all this inserts, the context is the same and it is getting bigger, … Read more
Please try this to refresh a single entity: Context.Entry<T>(entity).Reload() Edit: To get fresh data for a collection of entities is worth trying to dispose the DbContext instance after each request.
Query syntax var query = from p in context.People group p by p.name into g select new { name = g.Key, count = g.Count() }; Method syntax var query = context.People .GroupBy(p => p.name) .Select(g => new { name = g.Key, count = g.Count() });
When using LINQ to Entity Framework, your predicates inside the Where clause get translated to SQL. You’re getting that error because there is no translation to SQL for DateTime.Add() which makes sense. A quick work-around would be to read the results of the first Where statement into memory and then use LINQ to Objects to … Read more
As the other answers have indicated, you need to use an anonymous type. As far as syntax is concerned, I personally far prefer method chaining. The method chaining equivalent would be:- var employee = _db.EMPLOYEEs .Where(x => x.EMAIL == givenInfo || x.USER_NAME == givenInfo) .Select(x => new { x.EMAIL, x.ID }); AFAIK, the declarative LINQ … Read more
A quick google points me to what looks like the solution. Open your EDMX in a file editor (or “open with…” in Visual Studio and select XML Editor). At the top you will find the storage model and it has an attribute ProviderManifestToken. This has should have the value 2008. Change that to 2005, recompile … Read more
The best way to refresh entities in your context is to dispose your context and create a new one. If you really need to refresh some entity and you are using Code First approach with DbContext class, you can use public static void ReloadEntity<TEntity>( this DbContext context, TEntity entity) where TEntity : class { context.Entry(entity).Reload(); … Read more
What would be the best approach to have high availability on my Entity Framework at anytime? You can go for a mix of pregenerated views and static compiled queries. Static CompiledQuerys are good because they’re quick and easy to write and help increase performance. However with EF5 it isn’t necessary to compile all your queries … Read more
The definition of these terms is quite ambiguous. You will find different definitions at different places. Entity: An entity represents a single instance of your domain object saved into the database as a record. It has some attributes that we represent as columns in our tables. Model: A model typically represents a real world object … Read more