EF Core Ignore existing tables in DB - entity-framework

I have a old DB that I need to create a new module for which will include some new tables.
The DB currently has no Entity Framework so I want to just add my new tables and just work with them and ignore the previous ones.
This post says in previous versions of entity framework you just need to do this:
add-migration Initial -IgnoreChanges
but EF Core does not support "Ignore Changes" as explained here.
I cannot find an alternative in Entity Framework Core.
How do I add my migrations for my specific tables whilst ignoring the rest of the existing database?

Related

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

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.

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.

Entity Framework Code First Don't Create Table

I'm using an existing database and I have mapped one of the tables as an entity (as i needed to map a foreign key).
So when it comes to initialising this database I would like EF to ignore this entity since it already exists.
How would I go about doing this?
You should create an empty migration as the first migration with the -IgnoreChanges flag to allow Entity Framework to get a snapshot of the existing database.
So out of the gate use:
Add-Migration InitialMigration -IgnoreChanges
and that will create a blank migration but it will update the Entity Framework metadata allowing the existing tables to exist and not be touched by migrations.
Also to be mentioned that the naming conventions that Entity Framework expects and your database schema may differ. You may need to manually setup the foreign keys using the Fluent API.
I didnt check with EF 6 specifically, but I think default EF behavious is that when the database exists, then it presume all model be ready and therefore will create no tables. If you want your initialization code to create tables with code first, use initialization code for prepare data. Look here :
http://www.entityframeworktutorial.net/code-first/seed-database-in-code-first.aspx

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.

Building DB schema from Entity Framework Model

I see that EF can update a model based on an existing database schema. However, I'm starting totally from scratch; I don't want to build tables, then rebuild them in the EF model file. Is there a way that I can draw out a model file, and have the SQL tables automatically be created for me?
Unfortunately, you have to wait for version 2 of EF.
Here is a link to the EF team's blog, where they talk about adding Model First support in v2:
One of the most painful omissions from the Entity Framework V1 was Model First, which basically means creating a conceptual 'model first' and then deriving a storage model, database and mappings from that.
[...]
The next release of the Entity Framework will include the ability to generate database schemas from your model. The main entry point into this feature is via the Designer context menu, to which we will add a new option called “Create Database from Model”.