A Web API project needs to be backported from Core 2.2 to .Net framework 4.6 and is developed in codefirst ; in the class of dbContext
there is OnModelCreating
some initial data but an error appears in the "hasData" line:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Generador>().**HasData**(
new Generador { Nombre = "xm324", Capacidad = 1000000 },
new Generador { Nombre = "Bee344", Capacidad = 3000000 },
new Generador { Nombre = "DCM445", Capacidad = 5000000 }
);
}
How is .NET Core 2.2 different from 4.6?
To insert data into your DB with EF 6 and .NET 4.6, you must follow these steps:
In the configuration file
Configuration.cs
inside the Migrations directory , you must override the methodSeed()
as follows:Next, create a migration to update the new data in the database.
UPDATE : Keep in mind that what the extender method
AddOrUpdate()
will do is insert (Insert) or update (Update) the records in the DB, each time an EF6 Update-Database is performed. So every time a migration is done , it will check if the logs exist, and if not, it will create them. In the event that the records already exist in the DB, it will only modify them if they are different.The HasData option is apparently only available in .Net Core starting from version 2.1, to plant data (Seed Data) in non-core versions of entity framework use the seed method or you can specify them in your migrations, create a migration specify to popular your tables: