I am creating some web app using ASP.Net Framework Core and Entity Framework Core.
I have created local database using entity framework core, I have seeded data.
The issue is that I need to re-seed the data, I need to have data with current dates for testing purposes.
I have created model and seeding in OnModelCreating method and it works fine for creating and seeding new database.
In OnModelCreating method, I am using HasData for seeding:
modelBuilder.Entity<User>().HasData(PatientMockDB.Users);
modelBuilder.Entity<VisitReview>().HasData(PatientMockDB.VisitReviews);
modelBuilder.Entity<Visit>().HasData(PatientMockDB.AllVisits);
In PatientMockDB static class I have new data that I want to have in db.
OnModelCreating method is run whenever I run the app and want to take something from db.
When I delete db, delete migrations then EF will create db with new data, but I would prefer not to do it everyday, again.
How to force data update without deleting db?
Additional question, what is the best way to omit running OnModelCreating when database is already generated with up to date data or when on production?
Related
I use EF Core 5. I created a few entities in my code (like Products, Users etc.). I have an existing SQL database called DbClients. Using EFCodeFirst approach I would like to create and add tables (based on my entities) to my existing database DbClients. I see that in most cases you are able to create migrations and then create db using add-migration and update-database. The issue is that I don't want to cerate database. Just create a few tables to current db. What commend do I need to use to make it possible?
I am trying to leverage scaffolding for EntityFrameworkCore to create an entity framework models for existing Sql Server database. This works fine and it did create the context file and all the tables.
However going forward I still want to continue to update the DB and get the model updated based on Database changes. This works fine with force flag on Scaffolding as it overwrites the tables with new changes.
The main concern I have is scaffolding needs db connection string which gets included in the DBContext file. And on running scaffolding command this file gets overwritten as well. Ideally i would like to save it in config file and have different value based on environments. However this means every time model is updated all my config changes are lost and I need to manually reapply the same
Is there any efficient way to do the same?
Use EF Core Power Tools, and the use the constructor that takes a DbContextOptionsBuilder and specify the privider and the corresponding connection string outside the DbContext class.
We are using Entity Framework Core 2.2 with code first. Sometimes I change one of the entities, but forget to create a new migration, or I create a migration but only in one context (we have for different db engines). I want to check it automatically (ideally as NUnit test) so it runs in our CI server for every commit.
Manually I would try to create a new migration and check that created Up() and Down() methods are empty. It there any way to do it as a NUnit test?
Where is difficulty in creating a test that :
Creates a new DB
Applies all current migrations to create a schema
Tries to use all the entities. It can be as simple as adding an entity, querying that entity and deep-comparing they are the same.
Drops the DB
If the schema doesn't have a migration for new entity or change in entity, you are sure to get and SQL error out of this.
Sure, every time you create a new entity, you would need to add a new test. But that should be already happening if you are using TDD.
And speed shouldn't be a problem either, as creating and dropping a DB shouldn't take more than few seconds and there won't be many of these kind of tests. And they can be parallelized.
If you want to get fancy and don't want to write test for each entity, it could be possible to do something like this :
Use reflection to get all entity types supported by a Context.
Use auto data generator like Bogus or AutoFixture to fill the entities with data.
Round-trip the entities through DB.
Compare the original with retrieved using deep-comparer like Compare-Net-Objects.
The usefulness of such automated approach would depend on complexity of your data model. Would just work for simple model. But would require lots of tweaking and overrides if the model is complex.
I have been using ADO.NET Entity Data Model Ef 6.x database first with different MS SQL Server databases and it has been working alright until I've got a prod local copy of production database schema having a lot of tables. I'm not doing anything fancy rather just trying to add DbContext with the standard wizard. For some reasons, it is taking ages and never successfully creates DbContext and entity models. It takes insane amount of time even when I try to select one single table out of whole lot of tables but it at least successfully creates DbContext. I need to create DbContext for pretty much every table within the database. Any thoughts how can I generate that using ADO.NET entity data model EF 6.x?
Following SQL script resolved my problem.
ALTER DATABASE SCOPED CONFIGURATION SET LEGACY_CARDINALITY_ESTIMATION=ON
Once you generate DbContext running the above script you should turn that back off
with the following script
ALTER DATABASE SCOPED CONFIGURATION SET LEGACY_CARDINALITY_ESTIMATION=OFF
We're in the process of converting a 15-year-old application into C#/Entity Framework Code First.
I've been able to create a migration for the table structures that I'll need, and I'd like to fill them with the data from our old application. Of course, it's possible to simply create a script using Management Studio, but I'd like to leverage Code First if possible.
I see that it's possible to reverse engineer a database schema. Is there a simple way that I could generate a migration or Seed method from existing data in a database? I'm not too worried about performance - it's enough data to be a pain to recreate by hand, but neither are we talking about thousands of rows.
Entity Framework itself should not be used for mass insertion/deletion/updating of records since the performance is really poor. If you want the seeding to be part of your migrations then you could include your SqlCommands inside your Seed method:
protected override void Seed(Context context)
{
context.Database.ExecuteSqlCommand("Command Here");
}
base.Seed(context);