C
C#7mo ago
cumslvt13

✅ What's the point of ModelSnapshot in ef core migrations

I'm trying to move my migrations to a separate projects in case I want to change a db provider. I've moved my existing migrations to the separate project and that's what I've got now:
Application.Core <-- ApplicationDbContext
Application.Migrations.MsSql <-- References Application.Persistence, contains migrations
Application.Core <-- ApplicationDbContext
Application.Migrations.MsSql <-- References Application.Persistence, contains migrations
In my Application.Core project I've added the following config for DI:
services.AddDbContext(o => o.UseSqlServer("connectionString"));
services.AddDbContext(o => o.UseSqlServer("connectionString"));
I didn't add x => x.MigrationsAssembly("WebApplication1.Migrations") call as it shown in the docs (https://learn.microsoft.com/en-us/ef/core/managing-schemas/migrations/projects) My questions is: how come EF still works even though it doesn't reference Migrations project where AppContextModelSnapshot is stored? And what are potential issues with my approach?
22 Replies
WEIRD FLEX
WEIRD FLEX7mo ago
i use a different assembly/project for migrations what do you mean by "ef core still works"
cumslvt13
cumslvt137mo ago
I mean that I still can query database altough project where dbcontext is doesn't reference project with migrations, which contains AppContextModelSnapshot which has modelbuilder setup
WEIRD FLEX
WEIRD FLEX7mo ago
migrations and snapshot aren't needed for dbcontext
cumslvt13
cumslvt137mo ago
so the modebuilder configuration is not actually used during runtime? Because this kinda confused me, because I can write the same configuration (for example for replationships) if I override it
Jimmacle
Jimmacle7mo ago
are you migrating your database at runtime? migrations are not your model, they describe how to modify your database when you change your model
JakenVeina
JakenVeina7mo ago
The ModelSnapshot is not used at runtime. It's used by the designer tool to generate migrations, and possibly some of its other functions the ModelBuilder configuration is very much used at runtime, and is also used to generate ModelSnapshots
cumslvt13
cumslvt137mo ago
No, migrations are being run in that separate project with migrations in CI Got it, thanks. Got confused because both BuildModel and OnModelCreating are accepting ModelBuilder
JakenVeina
JakenVeina7mo ago
where's BuildModel?
cumslvt13
cumslvt137mo ago
[DbContext(typeof(AppDbContext))]
partial class AppContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
// omitted
}
}
[DbContext(typeof(AppDbContext))]
partial class AppContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
// omitted
}
}
JakenVeina
JakenVeina7mo ago
hmm oh so, that method's not empty, right?
cumslvt13
cumslvt137mo ago
yeah, I've omitted it
JakenVeina
JakenVeina7mo ago
yeah, the snapshot is just another method of building a model so, like
cumslvt13
cumslvt137mo ago
I was confused because actually BuidModel can contain the same configurations as OnModelCreating can
JakenVeina
JakenVeina7mo ago
yeah, that's by design
cumslvt13
cumslvt137mo ago
But now I understand that it's purely for migrations
JakenVeina
JakenVeina7mo ago
so, when you run the designer tool to generate a migration
cumslvt13
cumslvt137mo ago
and those are actually generated from models + OnModelCreating
JakenVeina
JakenVeina7mo ago
it calls that BuildModel() method to generate a model, in memory, which represents (in thoery) the current state of the physical database and it does the same thing for DbContext.OnModelCreating() to generate a model, in memory, of whta you WANT the database to be then it can effectively diff those two object structures that's where the migration code comes from
cumslvt13
cumslvt137mo ago
Understood
JakenVeina
JakenVeina7mo ago
that file also gives you, as the developer, the chance to modify it, if you have anything to model that EF doesn't properly support same for the migration code itself
cumslvt13
cumslvt137mo ago
Guess my question is answered Thanks again for the explanation Remind me, how to properly resolve the question here There was a command or something
JakenVeina
JakenVeina7mo ago
uhhhhhhhhhhhhhhhh