How do I delete an object from an Entity Framework model without first loading it?

It is worth knowing that the Entity Framework supports both to Linq to Entities and Entity SQL. If you find yourself wanting to do deletes or updates that could potentially affect many records you can use the equivalent of ExecuteNonQuery. In Entity SQL this might look like Using db As New HelloEfEntities Dim qStr = … Read more

Group by Weeks in LINQ to Entities

You should be able to force the query to use LINQ to Objects rather than LINQ to Entities for the grouping, using a call to the AsEnumerable extension method. Try the following: DateTime firstDay = GetFirstDayOfFirstWeekOfYear(); var userTimes = from t in context.TrackedTimes.Where(myPredicateHere).AsEnumerable() group t by new {t.User.UserName, WeekNumber = (t.TargetDate – firstDay).Days / 7} … Read more

LINQ to Entities how to update a record

Just modify one of the returned entities: Customer c = (from x in dataBase.Customers where x.Name == “Test” select x).First(); c.Name = “New Name”; dataBase.SaveChanges(); Note, you can only update an entity (something that extends EntityObject, not something that you have projected using something like select new CustomObject{Name = x.Name}

LINQ to Entities does not recognize the method ‘System.TimeSpan Subtract(System.DateTime)’ method

You could use the EntityFunctions.DiffDays method EntityFunctions.DiffDays(product.EventDate, DateTime.Now) //this will return the difference in days UPDATE EntityFunctions is now obsolete so you should use DBFunctions instead. System.Data.Entity.DbFunctions.DiffDays(product.EventDate, DateTime.Now)

How to move from Linq 2 SQL to Linq 2 Entities?

To show the created SQL commands for debugging in EF using System.Data.Objects; … var sqlQuery = query as ObjectQuery<T>; var sqlTrace = sqlQuery.ToTraceString(); AFAIK there are no commands to create DB’s or do any sort of DDL work. This is design limitation of the “Entity SQL” language The EDMX design surface will map your current … Read more

How to avoid Query Plan re-compilation when using IEnumerable.Contains in Entity Framework LINQ queries?

This is a great question. First of all, here are a couple of workarounds that come to mind (they all require changes to the query): First workaround This one maybe a bit obvious and unfortunately not generally applicable: If the selection of items you would need to pass over to Enumerable.Contains already exists in a … Read more