I'm new to Entity Framework Core as I'm currently migrating over from EF6. We use a database first model.
I've created a .Net Standard class library where I've imported EF Core and have used the scaffolding command in VS to import one of my tables as a test - which worked.
We have a couple of different replica databases that we use for geo-redundancy and reporting purposed, but all have the same model.
I've set up the different connection options, somewhat like point 2 in the answer in this post Using Entity Framework Core migrations for class library project so we can support connections to the different databases.
Now I want to go about adding the rest of the tables in. I have seen mention of Migrations, but that seems to be for code first models.
I then tried to use the "-Force" command on the scaffolding, which did import an additional table, but I lost my multi-database support.
In EF6 I had this logic in the Context.tt file, so when I retrieved updates from the database it would retain the custom connection options I had.
Is there a way to replicate this in EF Core or something I am missing?
Also, for something as simple as a new column on a table, should I still run the same command?
Thanks in advance,
David
* UPDATED *
I ended up using EF Core Power Tools which is a great package and allows complete control over the model, much like the context.tt file used to.
For anyone looking in future, I reverse engineered and used the Handlebars templates in EF Core Power Tools. I pass the 3 connection strings I use in the startup of the application and can then set optional booleans to indicate which connection to use - an enum may be more elegant for anyone starting again but this makes migration easier for us. In the DbConstructor.hbs, I updated it to:
{{spaces 8}}public {{class}}(bool ReadOnlyDatabase = false, bool ReportsDatabase = false) : base()
{{spaces 8}}{
if (ReadOnlyDatabase)
_connectionString = ReadOnlyContext;
else if (ReportsDatabase)
_connectionString = ReportsContext;
else
_connectionString = ReadWriteContext;
{{spaces 7}} }
The DbContext.hbs file is:
{{> dbimports}}
namespace {{namespace}}
{
public partial class {{class}} : DbContext
{
public static string ReadWriteContext = "";
public static string ReadOnlyContext = "";
public static string ReportsContext = "";
private readonly string _connectionString;
{{{> dbsets}}}
{{#if entity-type-errors}}
{{#each entity-type-errors}}
{{spaces 8}}{{{entity-type-error}}}
{{/each}}
{{/if}}
{{{> dbconstructor}}}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(_connectionString);
}
{{{on-model-creating}}}
}
}