-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Need to create tables at runtime to support plugin solution #9238
Comments
I'm using |
Take a look at this blog post by Rowen Miller, I think this should help. |
@gordon-matt It seems like using Migrations at runtime, which if I remember rightly is what Rowan does in his post, could be a reasonable approach here. Beyond that, if existing tables never need to be modified, then you could look into using |
Thanks for the info. The blog post by Rowan Miller shows how to discover the classes that make up the model at runtime. That is something I've known how to do for a long time. He does also show how to invoke migrations from code:
That's only for EF6, so I found the following for EF Core:
However, that did nothing at all. I assume that is just supposed to apply the latest migrations that were created via the "Add-Migration" command. That's not helpful in my case, since there won't be any such migrations. Unless you know some way to create migrations inside class libraries (the plugins) and have them applied that way? I also looked at For now, I think I will have to look into the code for generating the database script, as shown in #2943. I will have to split that up and go through each "CREATE TABLE" statement, running only the ones where the table doesn't already exist. That's not very elegant, but I guess it will have to do until a better solution comes along. |
Alright, for those interested in a workaround, here is my solution. Be warned, it's a bit of a hack, but it works:
Then at the end of One issue is I need to still account for the parts of the script which are not This would be so much cleaner if |
Here's a much less hacky way of doing this; it's how I'm doing this in v5.0.11 (this method is a member of the inherited /// <summary>
/// Updates the database to ensure that all tables have been created
/// for which there are valid entities
/// </summary>
public void EnsureTablesCreated()
{
var infrastructure = this.GetInfrastructure();
var migSqlGen = infrastructure.GetService<IMigrationsSqlGenerator>();
var modelDiffer = infrastructure.GetService<IMigrationsModelDiffer>();
var conn = infrastructure.GetService<IRelationalConnection>();
var diffs = modelDiffer.GetDifferences(null, Model.GetRelationalModel());
var sql = migSqlGen.Generate(diffs, Model, MigrationsSqlGenerationOptions.Default);
foreach(var s in sql)
{
try
{
s.ExecuteNonQuery(conn);
} catch (Exception ex)
when (ex.Message.Contains("There is already an object named"))
{
// Ignore
}
}
} |
Is there currently a way to load schema from the database in form of IRelationalModel and compare it with the current DbContext schema ("design time")?
In your code, you are comparing with null IRelationalModel which yields all commands required to create the schema defined by DbContext. My use case is plugin architecture as well. |
I'm creating a project with a plugin architecture.. where each of the plugins may have entities that are added to the
DbContext
. That part is no issue - I can easily load in the new entity types at runtime, but the problem I am facing is that I have no way to automatically create the tables. EF migrations is not an option in this scenario, so I need another solution.EF can already generate the proper SQL to create the database, so I'm hoping it won't take too much effort to have something like this:
context.CreateTableFor<TEntityType>();
Even better: bring back the
EntityTypeConfiguration<TEntityType>
class from EF6 and you can do something like this:context.CreateTableFor<TEntityType>(EntityTypeConfiguration<TEntityType> configuration);
Those of us needing to create tables at runtime due to plugins, etc. could call such a method from the plugins on install for each table that needs to be created.
The text was updated successfully, but these errors were encountered: