C
C#2y ago
Alta

❔ Conditional data seed

Hi ! Currently working on an api where we're trying to manage two different builds (foo and bar) Foo is using an hellish hybrid database with a bit of SQL (ef core code first), noSQL and a Sequential indexed file database Bar is using a pure SQL database (EF Core code first) We already split the DAL so that services from Foo and Bar uses the same interface (calling their respective databases in their implementations) Foo and Bar are using the same migrations from our repository manager. But now, we want to use a set of initial data with Bar We though about doing a migration and adding some insert in raw sql in them But, considering that Foo and Bar are using the same migrations and that Foo doesn't want those data, we have to find a descent solution that would insert those into Bar while letting Foo untouched (descent meaning " at least maintainable short terms, if possible, maintainable long terms and ideally, not too hard to explain to newcomers in the team") A solution I dug up over that StackOverflow post https://stackoverflow.com/questions/37748859/is-it-possible-to-have-conditional-ef-migration-code sounds doable (but considering it is 6y old and has only 2 answers where the top answer has only 1 upvote,... Yeah, Idk) We'd have the migration in both Foo and Bar But the raw sql in the Up and Down would be wrapped in some preprocessor conditions Like so
protected override void Up(MigrationBuilder migrationBuilder)
{
#if Bar
migrationBuilder.Sql(@"
-- some raw sql where we insert data into the BAR database
")
#endif
}
protected override void Up(MigrationBuilder migrationBuilder)
{
#if Bar
migrationBuilder.Sql(@"
-- some raw sql where we insert data into the BAR database
")
#endif
}

If this solution doesn't look appealing to you... Yeah... To us neither... That's why I'd like to have your opinion on how to tackle that issue, if you will 🙂
4 Replies
Anton
Anton2y ago
I don't know much about the ways you can manage migrations in efcore, but I'd do like 3 folders, a common one and one for project specific migrations. Then you have to make sure they are called in the right order, so some order metadata would be necessary
Alta
Alta2y ago
That looks like a sound idea thank you 🙂
Anton
Anton2y ago
np
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.