One DbContext per web request… why?

NOTE: This answer talks about the Entity Framework’s DbContext, but it is applicable to any sort of Unit of Work implementation, such as LINQ to SQL’s DataContext, and NHibernate’s ISession. Let start by echoing Ian: Having a single DbContext for the whole application is a Bad Idea. The only situation where this makes sense is … Read more

Entity Framework VS LINQ to SQL VS ADO.NET with stored procedures? [closed]

First off, if you’re starting a new project, go with Entity Framework (“EF”) – it now generates much better SQL (more like Linq to SQL does) and is easier to maintain and more powerful than Linq to SQL (“L2S”). As of the release of .NET 4.0, I consider Linq to SQL to be an obsolete … Read more

EF Migrations: Rollback last applied migration?

I want to add some clarification to this thread: Update-Database -TargetMigration:”name_of_migration” What you are doing above is saying that you want to rollback all migrations UNTIL you’re left with the migration specified. Thus, if you use GET-MIGRATIONS and you find that you have A, B, C, D, and E, then using this command will rollback … Read more

Unable to update the EntitySet – because it has a DefiningQuery and no element exist

It usually happens because one of the following reasons: Entity Set is mapped from Database view A custom Database query Database table doesn’t have a primary key After doing so, you may still need to update in the Entity Framework designer (or alternatively delete the entity and then add it) before you stop getting the … Read more

Code-first vs Model/Database-first [closed]

I think the differences are: Code first Very popular because hardcore programmers don’t like any kind of designers and defining mapping in EDMX xml is too complex. Full control over the code (no autogenerated code which is hard to modify). General expectation is that you do not bother with DB. DB is just a storage … Read more

SqlException from Entity Framework – New transaction is not allowed because there are other threads running in the session

After much pulling out of hair I discovered that the foreach loops were the culprits. What needs to happen is to call EF but return it into an IList<T> of that target type then loop on the IList<T>. Example: IList<Client> clientList = from a in _dbFeed.Client.Include(“Auto”) select a; foreach (RivWorks.Model.NegotiationAutos.Client client in clientList) { var … Read more

MetadataException: Unable to load the specified metadata resource

This means that the application is unable to load the EDMX. There are several things which can cause this. You might have changed the MetadataArtifactProcessing property of the model to Copy to Output Directory. The connection string could be wrong. I know you say you haven’t changed it, but if you have changed other things … Read more

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