This is the the case
I have the following structure of my solution
WebApi => only APIS (no 3rd party libs not DI container no nothing except for apis)
DI.csporj => only for registration (doesn't have any infrastructure | 3rd party packages within it, except of course the Microsoft DI)
private void RegisterInfrastructureDataAccess()
{
_serviceCollection.AddScoped(factory => new DapperConnectionProvider(_dapperConnectionString));
_serviceCollection.AddScoped(factory => new StoreContext(_efCoreConnectionString));
}
Ef.csproj => only deals with read-write and nothing else. In this project I have the following
public StoreContext(string connectionString)
: this(GetOptions(connectionString))
{
}
internal StoreContext(DbContextOptions options)
: base(options)
{
RelationalDatabaseCreator creator = this.GetService<IDatabaseCreator>() as RelationalDatabaseCreator;
if (!creator.Exists())
{
creator.Create(); // /=> create database
creator.CreateTables(); /// => add database schema
}
/// TODO => see why this is breaking
Database.Migrate(); /// => apply migrations (this should creat db + schema and apply migrations if any)
this.Seed(); /// => seed with data
}
private static DbContextOptions GetOptions(string connectionString)
{
return SqlServerDbContextOptionsExtensions.UseSqlServer(new DbContextOptionsBuilder(), connectionString).Options;
}
What is happening.
First Run : Have nothing but the code . It will Create the DB and then the schema (tables and relations). It will then go and apply the migration using he Migration() method. In the end it will Seed the DB.
Now when I do some changes in some table, and run Add-Migration MyTable_New_Col for example, I get this
System.Data.SqlClient.SqlException (0x80131904): There is already an object named 'TableName' in the database.
...... ClientConnectionId:b5001375-eafe-4f41-ab76-baaa290794f0 Error Number:2714,State:6,Class:16 There is already an object named 'TableName' in the database.
Now If I try a different approach
1st : I have no database and run this
internal StoreContext(DbContextOptions options)
: base(options)
{
Database.Migrate(); /// => this should and this is what the API summary says "Applies any pending migrations for the context to the database. Will create the database if it does not already exist."
this.Seed(); /// => seed with data
}
I will only get the __EFMigrationsHistory table, So my schema will not be applied and the error message that I get is the following
Invalid object name 'SomeOtherTableName'.
I want to do migrations programmatically, not by running Update-Database command.
The flow.
WebApi is my startup project. It will use the DI for dependency resolution. I make a call to an api. I get the StoreContext Instance as configured in DI, It then goes in the public constructor which calls the internal one. It checks if DB and schema exists, it creates if not, IT SHOULD APPLY MIGRATIONS (but throws exception) and it should seed if necessary
Why is this braking ? what is the solution ?
User contributions licensed under CC BY-SA 3.0