Extra table in database Entity Framework Code First With Database Migrations - c#

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.....")

Related

Entity Framework Core Database First

My team has inherited a database application that contains hundreds of tables. The application uses Entity Framework and takes a database first approach for development. Our current process is to pull a table or two at a time into the edmx using the Update Model From Database... tool.
We are considering making a new API with .Net Core, but as far as I can tell from the research I have done, there is no equivalent process in the Entity Framework Core tools. The closest thing I can find is to reverse engineer the entire database with Scaffold-DbContext, and then use migrations for all future database changes. We can't scaffold the entire database, because some of the tables have errors, and fixing all those errors is not a viable option for us right now.
I have found that I can supply a list of tables that I want scaffolded with the initial Scaffold-DbContext call, but I'm not sure if migrations can be used in a similar way to the Update Model From Database... tool. Can I use migrations to add tables that already exist in our database? If not, what other options should I be looking at?

Does NHibernate have a feature to Create and Update Tables, like EF Code First does Automatic Migrations?

I am using NHibernate and Fluent NHibernate for the first time. I had to physically create the database and write SQL scripts for the first 2 tables. Now, i am wondering if this technology (NHibernate), like Entity Framework (Code First) when you have enabled migrations has a similar too to create and update tables?
This is what i mean by Ef Code First Automatic Migrations
nHibernate does support schema changes using SchemaUpdate
See: Is NHibernate SchemaUpdate safe in production code?
You can also generate a create script to execute against your database using nHibernate SchemaExport.
SchemaUpdate is not recommended for production use because of the security privileges that have to be granted in order for this to work. Personally I think you should look at a code based migrations tool which are designed to handle initial database creates plus full revision control, I use Migrator.NET - Database migration in C#

Why is child table data not retrieved after renaming the table in SQL Server?

In SQL server I renamed my table TrialOrderStockNumbers to TrialOrderStock. A foreign key points back to the TrialOrders table. In the EF designer I deleted the TrialOrderStockNumbers entity and ran the Update Model From Database wizard (adding back the table TrialOrderStock). When I run the application the child data is now missing.
Now in SQL Profiler no query executes to get the Stock data. Before making the change profiler showed a statement selecting the data. I have had to discard all my changes and (for now) I plan to leave the old table name.
I'm using Entity Framework database first. Why did Entity Framework stop retrieving child data after renaming the table in SQL Server and updating the model?
It has been a while since I worked with MEF, where I had a similar issue with the entities getting out of sync with the database schema. As far as I recall, I simply re-generated the domain service and the problem was resolved.

Mapping and sql database creation

I started a project and use Entity Framework 5.
Now I created a database on my SQL Server Express with all tables.
Further I created the DbContext with a fluent mapping.
What is better, the fluent mappings or the .edmx mapping files?
The database is now on the SQL Server but I want support also SQL local db.
Is there a way to tell the EF that I want to use a SQL local db?
Should I ship the whole database within the setup or better to create the database on startup of my application? How can I use EF to create the database (SQL server or SQL local db)?
Every useful help would be appreciated.
If you are using EF5 I would stay away from edmx.
You could reverse engineer your model from database using Entity Framework Power Tools.
Than you can customize your model using either Data Annotations or fluent mappings.
If you use EF5 code first it can create database automatically for you if not present, however that would not work very well on subsequent upgrades (it can recreate database but then you will use or your existing data). The options you could use, is either EF migrations, where you can specify in fluent-like languagage the modifications that were made to your database or use Database Project in Visual Studio, where you can store all your schema in source control and then generate database upgrade scripts betweeen releases.

Delete a specific EntityFramework Migration

Is there a way to delete an automatic migration in the migration history of a project using Entity Framework Migrations? This is a code-based EF project that's has a mix of explicit and automatic updates. There is an automatic update that deletes a table of content and isn't necessary. I'd like to be able to just remove it.
For some reason, the database it's running against doesn't have a __MigrationHistory table. (At least I don't see it in the list of tables for the databases or the list of System Tables in the database. Not sure if you have to enable viewing those somehow or not inside SSMS).
Please offer any advice you have. I'm open to whatever solution as long as I can maintain all that application's data.
I hope it is not too late.
The __MigrationHistory table is made in System Tables section on Target Database(Same as base mode Database)

Categories

Resources