I'm trying to use Entity Framework Core in a data repository, but I'm having trouble to have EF create the database.
In the previous versions of EF it all happened automatically, EF created the database if it didn't exist on the first time one tried performing any operation on the data.
Now, I've seem on the docs the instruction to use EF Migrations. The instructions are to install the dependencies and tools:
And to run dotnet ef migrations add MigrationName
together with dotnet ef database update
.
Now here comes the thing. As I'm building a repository, I have a project AppName.Data
where the repositories are implemented and a project AppName.WebApi
where the repositories are consumed.
If I try to add the tools and run dotnet ef
from AppName.Data
it doesn't work because AppName.Data
is a library. The error message says that the tool can only be used in the context of one executable app.
The next try was to run dotnet ef
from AppName.WebApi
, which is executable. Now dotnet ef
works but if I try dotnet ef migrations add InitialMigration
to create the database I get one error, because the DbContext
is not on the AppName.WebApi
project, but on another project.
I really have no idea how to do this. So if I want EF to create the database and all of that, and I want to use repositories, so that there is no EF specific code on the executable project , what should I do?
After searching about it, I've found on the docs , on the "Preview 2 Known Issues" that this is currently a limitation of the tools. It does require one executable app project to be able to run, because it needs to use dotnet run
.
The docs also provides workarounds. The one I prefered and that worked for me was the first workaround. We simply point to one executable app project passing the --startup-project
with the path of the project.
So instead of running
dotnet ef migrations add MigrationName
We run
dotnet ef --startup-project pathToProject migrations add MigrationName
and everything works as expected.
Alternatively in your repository you can add the following code to your DbContext class:
public InviteesDbContext(bool recreate = false)
{
if (recreate)
{
recreateDatabase(this);
}
}
private static void recreateDatabase(InviteesDbContext dbContext)
{
dbContext.Database.EnsureDeleted();
dbContext.Database.EnsureCreated();
setPermissions(dbContext);
seed(dbContext);
}
To activate this code and to re(create) your database at will, in a console project added to the solution that includes your datarepository project use this code.
static void Main()
{
using (InviteesDbContext dbContext = new InviteesDbContext(false))
{
Console.WriteLine("Database in existence or created");
Invitee invitee = dbContext.Invitees.Where(i => i.Id == 1).SingleOrDefault();
if (invitee != null)
{
Console.WriteLine("Invitee " + invitee.Id.ToString() + " found !!");
}
Console.ReadKey();
}
}
If you temporarily change to
using (InviteesDbContext dbContext = new InviteesDbContext(true))
and run the console application the database will be re(created).
Warning : Only use this code in earlier phases of your project when the database is changing regularly and the potential loss of data is not so important. Never use this code in production.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.