Categories
Programming

Execute Raw SQL Scripts in Entity Framework Core

Most of the time, Entity Framework Core will just do the right thing. Every now and then, though, you’ll find that it’s doing something in a bit of a sticky way and you’ll want to take control. Usually it’s when you’re deleting a range on a table with cascading deletes. Here’s an example of the […]

Categories
Programming

Entity Type IdentityUserLogin Requires a Primary Key

You will usually come across this problem in .net Core Entity Framework when you first override the OnModelCreating method in your DbContext. The entity type ‘IdentityUserLogin‘ requires a primary key to be defined. If you intended to use a keyless entity type call ‘HasNoKey()’. If you check your DbContext class, you’ll see that when you […]

Categories
Programming

.NET Core Entity Framework Migrations

When you don’t want to script out your own database, Entity Framework Core has your back. When you change your database context or the models it uses, you can use a couple of commands to create and update your database to keep it in sync with your model. These examples run in the Package Manager […]

Categories
Programming

Stale Data in Entity Framework

There is a subtle bug that you can encounter when using Entity Framework on a database where updates can happen out of band. For example, the situation I discovered this issue involves a replicated SQL database, with Entity Framework running in “read only” mode against the replication subscriber databases. The data is changed in the […]

Categories
Automation Programming Visual Studio

Using an In Memory Database as a Test Double with Entity Framework

If you are using Entity Framework and you want to test your application without the real database, the typical technique employed is to write a repository or query provider that you can substitute with a stub during the test. Sometimes, though, you are better off pushing things a couple of levels deeper. This is where […]

Categories
Programming

Getting the SQL Query From an Entity Framework IQueryable

If you have ever wanted to look inside an IQueryable in Entity Framework to see what query will be run, you’ll know that it is properly squirrelled away in there. On the whole, that’s a good thing because you shouldn’t care about it. Despite this, there are sometimes occasions where “never” doesn’t mean “never”. I […]

Categories
Programming

Mocking Entity Framework DbContext and IDbSet With Rhino Mocks

This is something that can take a little figuring out – using Rhino Mocks to mock a DbContext and the associated IDBSets. Because of this, I created a little generic method that helps a lot… private static IDbSet<T> GetDbSetTestDouble<T>(IList<T> data) where T : class { IQueryable<T> queryable = data.AsQueryable(); IDbSet<T> dbSet = MockRepository.GenerateMock<IDbSet<T>, IQueryable>(); dbSet.Stub(m […]