Can you manually add tables and references to SPs/Views/Functions in EF Core Code First to an existing DB? - entity-framework

In EF 6 my work flow was to make all DB changes directly in SQL Server and then manually update/add EF classes to match what's the in the database. What I want to avoid is driving the DB design from code or scaffolding from the DB into EF.
I just want to manually manage everything once the DBContext has been generated.
Is this still possible in EF Core?

I just want to manually manage everything once the DBContext has been generated. Is this still possible in EF Core?
Absolutely. Same as in EF 6 Code First, just create the classes and map them to your database objects.

Related

generate dbcontext objects from edmx with mappings

In our company we have a lot of legacy applications which use an edmx file together with EF4.
We would like to migrate to EF6 (or EF core). Is there a way that we can generate the code first objects (dbcontext, entities) from this edmx with the mappings.
How can we do this?
Maybe the best solution for this will be scaffold (reverse engineer your model) from current database.
EF Core has nice powershell tools for this scaffolding https://learn.microsoft.com/en-us/ef/core/get-started/aspnetcore/existing-db
You can also split the whole (maybe big) DbContext to more DbContexts with specified Table parameter
https://learn.microsoft.com/en-us/ef/core/miscellaneous/cli/powershell
EF 6 has also some options for scaffold actual database Entity Framework - Generating Classes
Why would you bother generating anything from .edmx? What if you created new project with code first from existing database?

Entityframework Core Migrations

I have a .net core 2 project, along with Entity framework Core.
I have an existing database, and I've mapped them out to database entities in code.
The problem is that when I add a migration, expectedly it picks up my entity as a new table, however it is an existing table. The table shares the same name.
I suppose, I could just remove the contents of the Up method of the migration, but I want to know if there is a proper way instead of a workaround.
So, what I am asking is how can I tell entity framework core that this table is already existing?
EF 6 had an -IgnoreChanges option that would just take a snapshot with no Up() code, but that feature is not in EF Core (yet). See here.
If you comment out the Up() code as you have suggested that will indeed capture a snapshot of your existing objects and subsequent migrations will be incremental.

EF Context missing a collection

I'm trying to generate an entity from my SQL database using the ADO.NET Entity Data Model using the ADO.NET DbContext Generator. When I generate my edmx from the database I can see it in the model. I right click on my tt file (which is in a separate project) and run the custom tool. The entity appears. It's called CustomerContact. However, my dbcontext does not have a CustomerContacts collection. What is going on here?
I figured it out. The problem was the different versions of Entity Framework. I updated both projects to the latest version of EF and it works now

Entity Framework Migrations with a different Database

I am stuck trying to figure out how to set the database to run a migration on.
I have created a new empty project and set up Entity Framework using code first. I have all my classes built.
I want to add a new database and run the migrations on this. I have Migrations working but I can't figure out what database they are running on.
Is it possible to set the database you want to use for the migrations?
Multiple DBs for the same context gets a little tricky. But It is possible:
The essence of the problem is how EF decides which connection to use.
It will access instantiate the context without NO PARAMS during migration.
So depending on how that behaves influences you outcome and success.
Start here:
EntityFramework code-first custom connection string and migrations

EF CodeFirst - Create index after database create

I'm migrating my project from database-first to code-first.
Entity Framework does some nice work creating my new database (that should mimic the old one).
I'm using a combination of data annotations and the fluent API to describe my tables.
My database has a few indexes and I would like Entity Framework to create them as well. It seems the old way to do this is to define your own Initializer and use custom T-SQL.
But now that we have EF Migrations it should be easier to do so.
I can't seem to figure out how to combine CreateDatabaseIfNotExists<> with an automatic migration to create the indexes. I've tried to use the MigrateDatabaseToLatestVersion<,> but it doesn't seem to perform the migration after the Database has been created.
What is the proper way to create indexes and constraints on database creation now that we have Entity Framework 4.3?
Don't use CreateDatabaseIfNotExists if you want to use migrations. Use MigrateDatabaseToLatestVersion from the beginning - it will create your database as well. Put your index creation code (calls to CreateIndex) into Up method of your migration class.
If you already have existing database and you want to use migrations you must first create initial migration.