Posts

Showing posts with the label Entity Framework

Make ‘dotnet ef‘ work with EF Core 2.0

Image
EF Core 2.0 changed the way the CLI looks for the DBContext to use. There are now two ways: Invoke BuildWebHost in Startup.cs Use the class implementing IDesignTimeDbContextFactory<T> BuildWebHost is called even if a class implements IDesignTimeDbContextFactory<T>. Solution for this problem: Rename BuildWebHost to e.g. _BuildWebHost (making it private isn’t enough). Next step: create the class DesignTimeDbContextFactory : public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext> { public ApplicationDbContext CreateDbContext(string[] args)    {     IConfigurationRoot configuration = new ConfigurationBuilder()           .SetBasePath(Directory.GetCurrentDirectory())           .AddJsonFile("appsettings.json")           .Build();       var builder = ne...

Creating a basic MVC 6 web application with Entity Framework 7 and xUnit - Part 2

Image
In this blog post we will connect to an actual database, create the tables using the code-first approach and add some initial data. Be aware that migrations seem to be a bit buggy at the moment, but my solution was (at least for me) the most stable one. You can clone the repository with this sample from Github . 1. Creating a database First, open the sql server object explorer (in the V iew  menu). I use the default MSSQLLocalDB for this example, but you can use any running SQLServer. Now add a new database to this SQL Server. Specify a database name and the location where to put the database file. Finally, we need the connection string for this database. Get to the properties of the newly created database and copy the connection string. 2. Necessary changes before the migrations Before we can run any migration, we have to set up some things. Create a Config.json in the Sample1 project and add your connection string. Attention: you have to escape the bac...

Creating a basic MVC 6 web application with Entity Framework 7 and xUnit - Part 1

Image
In my first post I'm showing you how to set up a basic MVC 6 web application with Entity Framework 7. Before we do any controllers, or creating any databases, we use xUnit and moq for creating a mock database context and testing our service. You can clone the repository with this sample from  Github . 1. Creating the projects Open Visual Studio 2015 RC and select New project . Make sure you choose .NET Framework 4.6. Now create an empty ASP.NET 5 project. Now create two projects of type Class Library, Sample1Data and Sample1DataTests : You should now have 3 projects in your solution: Next we need to install MVC 6, Entity Framework 7 and xUnit. To do this, we don't use the NuGet UI. It's easier to edit the project.json file of each project and adding the references there. Enjoy the Intellisense ;). Begin with the Sample1 project. Make sure you dependencies section looks like this: As you can see, we already added a dependency to our Sample1Data pro...