Command Timeout with Entity Framework 4.1 Code First

I found this solution after another Google search. You can access the ObjectContext for a DbContext by casting this to an IObjectContextAdapter. From http://social.msdn.microsoft.com/Forums/en-ZA/adodotnetentityframework/thread/6fe91a64-0208-4ab8-8667-d061af340994: public class MyContext : DbContext { public MyContext () : base(ContextHelper.CreateConnection(“my connection string”), true) { ((IObjectContextAdapter)this).ObjectContext.CommandTimeout = 300; } }

Entity Framework creates foreign key objects instead of using those that are already available

If you don’t use the same context instance to load related entities you cannot simply add them to the new entity and expect that existing records in the database will be used. The new context doesn’t know that these instances exist in the database – you must to say it to the context. Solutions: Use … Read more

Enums with EF code-first – standard method to seeding DB and then using?

Unfortunately, enums are not natively supported on EF 4.1. Here’s one rather known article on how to deal with them: Faking enums on EF 4. It does, however, require a wrapper. There’s a simpler way to map enums in EF 4 however: just create an int property on your class to represent the int value … Read more

Entity Framework code first. Find primary key

You can ask mapping metadata to get names of key properties (there can be more then one): ObjectContext objectContext = ((IObjectContextAdapter)dbContext).ObjectContext; ObjectSet<YourEntity> set = objectContext.CreateObjectSet<YourEntity>(); IEnumerable<string> keyNames = set.EntitySet.ElementType .KeyMembers .Select(k => k.Name); Once you have key names you can use reflection to access their values. As you can see the approach reverts back to … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)