As part of a CI implementation I would like to use the migrate tool that ships with Entity Framework 6 to run migrations (note I do not want to run them automatically).
I have the migrate tool in the bin folder of my site and run:
migrate assembly_with_configuration_type.dll /startupconfigurationfile="path_to_web.config"
However this gives me: ERROR: The type initializer for 'System.Data.Entity.Migrations.DbMigrationsConfiguration' threw an exception. The assembly that I specify in the command is NOT the MVC web application, but is an assembly from a referenced project within the web application.
Start of my configuration class:
public class Configuration : DbMigrationsConfiguration<CustomDbContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
SetSqlGenerator("MySql.Data.MySqlClient", new MySqlMigrationSqlGenerator());
}
}
The customdbcontext is a plain class that is resolved on app_start using LocalIocManager.Resolve. Not sure what's going on.
Related
I have a solution with the following projects:
ASP.NET Core MVC website
ASP.NET Core Web API
DAL [incl DbContext and model classes]
Both the website and API reference the DAL and can use it's dbContext and model classes.
Both these projects have an appsettings.json file with the connection string and DB access is all working.
I can't how ever work out how to add migrations.
If I set the default project to the DAL project, I get this error:
Unable to create an object of type 'myDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
If I set it to the Api or website, I get:
Your target project 'website' doesn't match your migrations assembly 'DAL'. Either change your target project or change your migrations assembly.
Change your migrations assembly by using DbContextOptionsBuilder. E.g. options.UseSqlServer(connection, b => b.MigrationsAssembly("website")). By default, the migrations assembly is the assembly containing the DbContext.
Change your target project to the migrations project by using the Package Manager Console's Default project drop-down list, or by executing "dotnet ef" from the directory containing the migrations project.
I can find information on how to set up a separate class lib for the dbcontect and models for .NET Core 3.1 but I am struggling with .NET 6.
I have tried to follow this tutorial
https://garywoodfine.com/using-ef-core-in-a-separate-class-library-project/
But it's not the same as .net 6 so I cant follow it as I get errors when I try and create the DBContextFactory towards the bottom. The SetBasePath is not a definition within ConfigurationBuilder. I'm not fluent enough with .NET Core to work out whats required.
I have the exact same setup as you have with a website, api and a DAL layer.
This is how I register the DbContext in the website and in the api program.cs:
builder.Services.AddDbContext<AppDbContext>(
options => options.UseSqlServer(config.ConnectionString, sqlOptions => {
sqlOptions.MigrationsAssembly("App.Entities");}));
This is how the AppDbContext is created in the DAL project:
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{ }
// Your DbSet entities here
}
Then when you run your first migration command "Add-Migration" you need to select the DAL project in the package manager console dropdown and it should automatically create the migration folder with the migration files.
You need to have the following packages installed in the DAL project:
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools
Honestly, one would need more information to narrow down the problem.
I think there are two possible problems.
You are not using the exact DB Context in the program.cs of the two project in the presentation layer(MVC and API).
You have updated the migration the migration on API or MVC, or you have made changes to the database hence the comparison is not as clear.
How to resolve:
Make sure that database connection string in the two application.json
is same
Make sure that in the Program.cs of the the API and the MVC, that you have added the exact dbcontext from DAL "and is identical".
Make sure you have uniform Nugget package across the solution for the EF tool, Server, Design
Make sure the Models in the last migration mirrors what is obtainable in the database.
Make sure that the Package Manager console is has DAL selected as the default project, while the startup project which can be MVC or API is not having any errors.
Make sure that there is a dbSet<> in the dbContext in DAL before adding a migration
I am setting up a new ASP.NET Core 2.0 Razor Pages project, and I want to reuse an existing class library project that has my EntityFramework model, DBContext etc.
Prior to ASP.NET Core 2.0, the approach I took to use a separate EntityFramework project in an MCV website project was:
To add the EntityFramework project to the solution that has my website project
Add a reference to the EntityFramework project in the website project
Copy the connection string from the "app.config" file in the EntityFramework project to the "web.config file" file in the website project.
I am still getting to grips with ASP.NET Core 2, and it seems this approach does not work anymore. "web.config" is now "appsettings.json", and it also seems changes need to be made to the "startup.cs" file.
I have tried the following approach (based on this blog post:
http://jakeydocs.readthedocs.io/en/latest/data/entity-framework-6.html)
I copied the connection string from the EntityFramework project into "appsettings.json":
"ConnectionStrings": {
"BjornsonEntities": "metadata=res:///BjornsonModel.csdl|res:///BjornsonModel.ssdl|res://*/BjornsonModel.msl;provider=System.Data.SqlClient;provider connection string="data source=COMPUTER_NAME;initial catalog=DB_NAME;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""
}
I included a second constructor for my EntityFramework DBContext class, that takes the connection string:
public partial class BjornsonEntities : DbContext
{
public BjornsonEntities()
: base("name=BjornsonEntities")
{}
public BjornsonEntities(string nameOrConnectionString) : base("name=BjornsonEntities")
{}
}
And finally I added the following line to ConfigureServices in startup.cs:
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped((_) => new BjornsonEntities(Configuration["Data:DefaultConnection:ConnectionString"]));
services.AddMvc();
}
However, when I then try to execute some code to get data from the EntityFramework project, for example like this:
var c = new BjornsonEntities();
then execution fails in the first DBContext constructor
public partial class BjornsonEntities : DbContext
{
public BjornsonEntities()
: base("name=BjornsonEntities")
{}
I get the following error message:
The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception.
FileNotFoundException: Could not load file or assembly 'System.Configuration.ConfigurationManager, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.
Any suggestions for what goes wrong here? And more specifically, what is the best way to make use of an existing EF project (that is not EF Core) in a separate project? Is there an equivalent, straightforward three-step approach, like the one I described earlier (that works for non ASP-NET-Core projects...)?
UPDATE
Initially, the web application project was set up as a .NET Core 2.0 project, not .NET Framework. When the consuming ASP.NET Core web application project is set up as a .NET Framework project the DBContext initialisation mentioned above runs. However, when use the DBContext to try a LINQ query such as the one below
var c = new BjornsonEntities();
var result = c.PersonEvaluationResponses
.Where(p => p.PersonEvaluation.ProjectID == 14)
.Select(p => p).ToList();
I get an error that "No connection string named 'BjornsonEntities' could be found in the application config file". There is a connectionstring with this name in appsettings.json, so clearly I'm doing something else wrong, and the connectionstring is not picked up.
In my ASP.NET EF 6 app, I have the following Configuration:
internal sealed class Configuration : DbMigrationsConfiguration<Gcim.Management.Module.BusinessObjects.ManagementDbContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
protected override void Seed(Gcim.Management.Module.BusinessObjects.ManagementDbContext context)
{
}
}
In my project, I have EF code-first migrations enabled.
I'm able to run add-migration, as well as update-database from the VS Package Manager prompt.
However, when my entities change, adding a migration, and updating database only works in dev environment.
When I publish my ASP.NET project, install it on production IIS, and run, I still get this error:
The model backing the 'ManagementDbContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).
What else do I need to do in order to force the incremental DB changes in production, without data loss?
You can execute a given migration file against a given environment (in this case, your production environment) by using the migrate.exe application included as part of the Entity Framework nuget package. You'll need to execute your latest migration against your production environment before publishing your web application - once this has been done, the newly deployed application will work correctly.
You do this by specifying the connection string and .dll containing the migration class(es), as well as possibly a specific migration. For example:
Migrate.exe Gcim.Management.Module.BusinessObjects.dll /connectionString="Data Source=[YourProductionDatabase];Initial Catalog=[YourTable];Integrated Security=SSPI" /connectionProviderName="System.Data.SqlClient" /targetMigration="[NameofYourMigration]"
Documentation on migrate.exe can be found here: https://msdn.microsoft.com/en-us/data/jj618307.aspx
How do you add and use an SQLite database in an ASP.NET Core web application, using EntityFramework 7 ?
I dived into ASP.NET Core the moment I heard about it and created my first web application, I suddenly had a bunch of data that I wanted to store and SQLite seemed like the obvious choice.
Since I wanted it to stay with my application, keep it lightweight, simple and avoid setting up a separate database.
So how would one go about creating an SQLite database in ASP.NET Core?
ASP.NET Core - now formerly known as ASP.NET MVC 6
EntityFramework Core - now formerly known as EntityFramework 7
Update: November 4th, 2016.
Reformatting - pictures to code examples.
Info:
Keep in mind that in some code examples, code that was generated by the visual studio template have been omitted.
Update: July 11th, 2016.
.NET Core and EntityFrameWork Core version 1.0 is upon us!
So this guide deserves a little update
Step 1:
Create your application.
Step 2:
Get the necessary packages
Microsoft.EntityFrameworkCore 1.0.0
Microsoft.EntityFrameworkCore.SQlite 1.0.0
Step 3:
Create your context:
(The Context will be a class that you create)
public class DatabaseContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Filename=MyDatabase.db");
}
}
Step 4:
Add your context to your services:
(Located in your Startup class)
public void ConfigureServices(IServiceCollection services)
{
services.AddEntityFrameworkSqlite().AddDbContext<DatabaseContext>();
}
Step 5:
Create your database on startup, by adding it to the startup method
(Located in the Startup class)
public Startup(IHostingEnvironment env)
{
using(var client = new DatabaseContext())
{
client.Database.EnsureCreated();
}
}
Et Voíla!
Now you will be able to use SQLite in your ASP.NET Core applications.
The old guide still applies regarding how you create your models as well as using your database context.
Update: May 28th, 2016.
.NET Core RC2 and EntityFramework Core RC1 have been released.
They have improved and simplified the steps for setting up SQLite.
But I'm experiencing some trouble with it and can't replicate it, because of an error with the Newtonsoft.Json library and NuGet.
I recommend sticking to the RC1 libraries if you want to do this, for now!
Step 1:
Create your ASP.NET web application
Step 2:
Go to Tools -> Nuget Packet Manager -> Manage Nuget Packages for Solution.
Search for EntityFramework.SQLite and check the Include prelease box.
Install the package
Step 3: Creating a context
Create a context class for your database.
Call it whatever you want, but let's go with something that's customiary, like MyDbContext.
Make your new class inherit the DbContext class and override the OnConfiguring method and define your connection like so:
public class MyDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var connectionStringBuilder = new SqliteConnectionStringBuilder { DataSource = "MyDb.db" };
var connectionString = connectionStringBuilder.ToString();
var connection = new SqliteConnection(connectionString);
optionsBuilder.UseSqlite(connection);
}
}
Step 4:
Go to the Startup.cs and make sure your database is created at the start of your web application:
public Startup(IHostingEnvironment env)
{
// Set up configuration sources.
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
using (var db = new MyDbContext())
{
db.Database.EnsureCreated();
db.Database.Migrate();
}
}
Secondly we need to add the service:
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
services.AddEntityFramework()
.AddSqlite()
.AddDbContext<MyDbContext>();
}
Step 5: Defining your Models
Create your models and go to MyDbContext.cs and add a new property for each of your new models (given that you want a table for each!)
Here's an example:
My Model:
public class Category
{
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string UrlSlug { get; set; }
}
Adding it to my context:
public class MyDbContext : DbContext
{
public DbSet<Category> Categories { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var connectionStringBuilder = new SqliteConnectionStringBuilder { DataSource = "MyDb.db" };
var connectionString = connectionStringBuilder.ToString();
var connection = new SqliteConnection(connectionString);
optionsBuilder.UseSqlite(connection);
}
}
Step 6: Using the the context
Go to your HomeController and add a new field to your controller.
private readonly MyDbContext _myDbContext = new MyDbContext();
And use it in an ActionResult by passing it to the returned view:
(Now lets assume we have a category in our database)
public IActionResult Index()
{
var category = _myDbContext.Categories.First();
return View(category);
}
So by going to your Index view, you can use our imaginary data from the database. By defining a model in the top of your view like so:
#model MyNameSpace.Models.Category
#{
ViewData["Title"] = "Hey Ho! SO!";
}
<div class="page-header">
<h1>#ViewData["Title"]</h1>
</div>
<div class="container">
#Model.Title
</div>
Now by starting our web application and going to the assigned address we should see a default html page with a fancy bootstrap header, showing this on the page:
The second line is (or would be) the title of our first category in our database.
Entity Framework 7 Docs
This is my first Q&A - if you have any input or something that needs clarifying don't hesitate to comment.
This is a very basic example of how to implement an SQLite database into an ASP.NET Core MVC web application.
Do note that there is several ways to set the connection string for the database, how to use the context and that EntityFramework 7 is still a prerelease
If you want to create an ASP.NET Core web application using SQLite for the database, I highly recommend using Yeoman to scaffold the app for you. You need to first install .NET Core 1.1 SDK (Visual Studio 2015 seems to only include SDK versions 1.0.0 and 1.0.1 at the moment). You then need to install Node.js which comes with npm and then install the following npm packages: yo and generator-aspnet. Then all you have to do is run yo aspnet and answer a few questions.
C:\Development>yo aspnet
? ==========================================================================
We're constantly looking for ways to make yo better!
May we anonymously report usage statistics to improve the tool over time?
More info: https://github.com/yeoman/insight & http://yeoman.io
========================================================================== Yes
_-----_ ╭──────────────────────────╮
| | │ Welcome to the │
|--(o)--| │ marvellous ASP.NET Core │
`---------´ │ generator! │
( _´U`_ ) ╰──────────────────────────╯
/___A___\ /
| ~ |
__'.___.'__
´ ` |° ´ Y `
? What type of application do you want to create? Web Application
? Which UI framework would you like to use? Bootstrap (3.3.6)
? What's the name of your ASP.NET application? WebApplication
Afterwards, you will get the following response:
Your project is now created, you can use the following commands to get going
cd "WebApplication"
dotnet restore
dotnet build (optional, build will also happen when it's run)
dotnet ef database update (to create the SQLite database for the project)
dotnet run
Run dotnet restore, dotnet ef database update, and then dotnet run and go to localhost:5000 to make sure the project is running.
Now you can open the project in Visual Studio 2015 (assuming you're on Windows) or Visual Studio Code.
The great thing about this is that Startup.cs, project.json, and appsettings.json files are setup to use SQLite. Also, a SQLite database is created for you:
Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));
}
project.json:
{
"Microsoft.EntityFrameworkCore.Sqlite": "1.1.0",
"Microsoft.EntityFrameworkCore.Sqlite.Design": {
"version": "1.1.0",
"type": "build"
}
}
appsettings.json
{
"ConnectionStrings": {
"DefaultConnection": "Data Source=WebApplication.db"
}
}
Your SQLite database will be located in bin/Debug/netcoreapp1.0. In my case, it is located in C:\Development\WebApplication\bin\Debug\netcoreapp1.0\WebApplication.db
If you want to rename the SQLite database, modify appsettings.json file and run dotnet ef database update.
To learn more about using SQLite database with .NET Core and EF Core, check out this article: .NET Core - New Database
Install Below mentioned packages
PM> Install-Package Microsoft.EntityFrameworkCore
PM> Install-Package Microsoft.EntityFrameworkCore.Sqlite
PM> Install-Package Microsoft.EntityFrameworkCore.Tools
Create Models
Create DBContext class add SQLite connection configuration
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlite("Data Source=DBFileName.db");
Run migration commands to start using it
PM> add-migration <MigrationName> //Ex: add-migration IntialMigration
PM> update-database
https://fullstack-lab.co.in/Sqlite-entity-framework-core-quick-start
This article provides simple steps to use SQLite with Asp.net core 3.1
In dotnet 6 :
Your DbContext constructor should look like this: (remove OnConfiguring method from your DbContext.
public PaymentDbContext(DbContextOptions<PaymentDbContext> options) : base(options)
{
}
and in program.cs file, add your service like this:
builder.Services.AddDbContext<PaymentDbContext>(options =>
options.UseSqlite($"Data Source={dbPath}"));
dbPath is your database address.
if you want to update your database and your dbContext file located in a different solutions don't forget to use --startup-project in dotnet ef database update command :) ex:
dotnet ef database update --startup-project ../PaymentProject.Api/PaymentProject.Api.csproj
All my searches are coming back to the same one of two issues, which aren't the problem in this project. I've never had trouble with this before, but this particular project is being weird.
First off, the project name is Site. The class SiteContext inherits DbContext.
When I run Enable-Migrations, even explicitly , the Package manager console returns an error:
PM> Enable-Migrations -ContextType SiteContext
The context type 'SiteContext' was not found in the assembly 'Site'.
But it's right there in the code:
// Not in a namespace or anything
public class SiteContext : DbContext {
// public DbSets in here
}
I can use SiteContext anywhere in my site's code, access the database though an object of it, etc. It's only the Enable-Migrations command that can't find it. Other than this, Entity's code-first functionality is working properly.
Any ideas what might be going on? Where did I mess this up?
(Entity Framework 6.0.2 / Web Pages 3.1.1)
ASP.NET Web Pages sites are built using the Web Site project type. This project type doesn't support Entity Framework Migrations. If you want to use Code First Migrations with an ASP.NET Web Pages site, you need to develop your data access code as a separate class library. That project type does support EF Migrations. I've blogged about it here: http://www.mikesdotnetting.com/Article/217/Code-First-Migrations-With-ASP.NET-Web-Pages-Sites
Instead of -ContextType, use -ProjectName and Specify Site.
Edit: First, try -ContextTypeName. I have to assume you didn't.