Debug code-first Entity Framework migration codes

I know that EF Code First Migrations is relatively new tool but don’t forget about you are still in .NET. So you can use: if (System.Diagnostics.Debugger.IsAttached == false) { System.Diagnostics.Debugger.Launch(); } After that you can see your InnerException. Or you can use try…catch statement like this: Exception handling Entity Framework

How to include a child object’s child object in Entity Framework 5

If you include the library System.Data.Entity you can use an overload of the Include() method which takes a lambda expression instead of a string. You can then Select() over children with Linq expressions rather than string paths. return DatabaseContext.Applications .Include(a => a.Children.Select(c => c.ChildRelationshipType));

EF LINQ include multiple and nested entities

Have you tried just adding another Include: Course course = db.Courses .Include(i => i.Modules.Select(s => s.Chapters)) .Include(i => i.Lab) .Single(x => x.Id == id); Your solution fails because Include doesn’t take a boolean operator Include(i => i.Modules.Select(s => s.Chapters) && i.Lab) ^^^ ^ ^ list bool operator other list Update To learn more, download LinqPad … Read more

Creating Composite Key Entity Framework

If Device table has composite primary key, then you need same composite foreign key on your NotificationMessageDevice table. How would SQL find Device without full primary key? Also you should make these fields to be part of NotificationMessageDevice table primary key. Otherwise you can’t guarantee primary key will be unique: public class NotificationMessageDevice { [Column(Order … Read more

Non-static method requires a target

I think this confusing exception occurs when you use a variable in a lambda which is a null-reference at run-time. In your case, I would check if your variable calculationViewModel is a null-reference. Something like: public ActionResult MNPurchase() { CalculationViewModel calculationViewModel = (CalculationViewModel)TempData[“calculationViewModel”]; if (calculationViewModel != null) { decimal OP = landTitleUnitOfWork.Sales.Find() .Where(x => x.Min … Read more

An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key

Edit: Original answer used Find instead of Local.SingleOrDefault. It worked in combination with @Juan’s Save method but it could cause unnecessary queries to database and else part was probably never executed (executing the else part would cause exception because Find already queried the database and hadn’t found the entity so it could not be updated). … Read more

Improve navigation property names when reverse engineering a database

There a few things you need to change inside the .tt file. I choose to use the third solution you suggested but this requires to be formatted like FK_CollectionName_RelationName. I split them up with ‘_’ and use the last string in the array. I use the RelationName with the ToEndMember property to create a property … Read more