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?
Related
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?
I have been asked to write a web site that will use an existing SQL Server database. The database was designed to work with another application, and so I can't make any potentially breaking changes to it.
Unfortunately, the database does not contain a single relational link, each table is standalone.
I know you can use EF commands to scaffold a database and create entity classes, but I would like the code to know the relationships that should exist between the tables.
I thought about scaffolding the database, then modifying the created classes to include the links, but I'm not sure if that would allow EF to load related entities. Also, as I will need to add some new tables to the database, I'm worried that EF will try and create those links when I do the migration.
Is there any way to do this?
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
I have searched but couldnt find any useful thing about generating automatically basic CRUD stored procedures when creating the database in code first approach. I am wondering is there a way to do this simply or i should create all the CRUD stored procedure by old fashion?
It is indeed possible to use Migrations with a Code First Database to generate stored procedures for CRUD operations in Entity Framework 6 and beyond. Note that this is not possible in this manner with Entity Framework 5 and earlier, though you could still use raw SQL calls.
Define your entity in the normal manner, and add a DbSet for the entity to your DbContext class.
Modify the OnModelCreating method of the DbContext to add the following entry: modelBuilder.Entity<SomeEntity>().MapToStoredProcedures();.
Using the Package Manager Console, generate a new migration with add-migration, i.e. add-migration SPGenerate.
Inspect the Up method of your migration to tweak the Stored Procedures to your liking.
Commit the migration to the database by issuing update-database in the Package Manager Console.
Is it okay to have a table in the database that is not one of the entities when using code first with database migrations? Or will this interfere with the migrations? I want to put in a table to track some miscellaneous information.
We do this with no issues. We do add the table through the migration though so we we can ensure all developers have the same schema. If you want to skip this and go via SQL Management studio then you should have no issued either.
Sql(#"CREATE TABLE.....")