How do you process data with Entity Framework Database First?

I see many seeding examples with First code, but I'm not sure I understand that the idiomatic way of seeding a database is when using EF Database for the first time.

+4
source share
1 answer

Best practice is highly dependent on the situation. Then there is a DEV and PROD environment. Automatically seed when using Drop and recreate when changing the model during DEV, so you have test data. This is when it is used the most.

Because of this, you can use the verification method that you run manually. I personally believe that the automatically-run seed method method is not so exciting and more useful for DEV prototyping when the database structure is unstable. When using migrations, as a rule, you save your data that you earned with difficulty. Some use seed at initial installation in PROD. Others will have a specific boot procedure that starts during the installation / commissioning process. I like to use custom boot routines.

EDIT: FIRST SAMPLE CODE. With DB First you just write to Db as usual.

// select the appropriate initializer for your situation eg Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyDbContext, MyMigrationConfiguration>()); Context.Database.Initialize(true); // yes now please //... public class MyMigrationConfiguration<TContext> : DbMigrationsConfiguration<TContext> where TContext : DbContext{ public MyMigrationConfiguration() { AutomaticMigrationsEnabled = true; //fyi options AutomaticMigrationDataLossAllowed = true; //fyi options } public override void Seed(TContext context) { base.Seed(context); // SEED AWAY..... you have the context } } 
0
source

Source: https://habr.com/ru/post/1480179/


All Articles