Cannot update Entity Framework model after schema name change - c#

I am on a project that has an EF model that was created with a schema of 'dbo'. At some point in time the schema was changed to something more meaningful. However, try as I may I cannot now update from the database with the newly named schema as all the entities create new objects instead of updating the current ones. For example instead of Person, I now get and Person1. I have tried to edit connection strings as well as the EDMX file but nothing seems to be working.
Is there anyway to get my model to update from the database with the renamed schema?
I am using EF 6.0 and .Net 4.5.

Related

Entity Framework remove all references to database for replacement with a new entity data model

I have an inherited project using Entity Framework, database first, where I need to remove all references to an Oracle database and replace with new references to a SQL Server database.
Adding the SQL Server plumbing is easy: "Add->ADO.NET Entity Data Model"... but since the old database and new database have so many (all) similar tables, there is a lot of overlap and I can't easily tell which references are to the old DB and which are to the new DB. No, I don't have the git history to find that commit and revert it, even if by hand.
My new approach is to completely remove all references to the old DB, not just the .edmx file but also every other file that was added to the project when the original Oracle Entity Data Model was added...but there is no "Remove->ADO.NET Entity Data Model".
Is there documentation anywhere of everything I will need to remove? Or to procedure to find all of those references?
I want to end up with code that has everything except the actual mapping to a database, at which point I'll add the new entity data model and, since the tables on the new server are named exactly the same as the old, the existing code will start working again with the new database/server.

Deleting columns from Database doesn't give a mistake in project

So there's an ASP.Net project that uses Entity Framework Core. Of course, there are Entities classes, Configuration Entities classes, etc. And there are properties in Entities that map some table's columns. Also there is connection string to connect to database. The thing I don't get: I deleted some columns from database table. But project builds and everything is fine. What do I miss? I though that mistake should be given because Entity properties don't map table columns...
A successful build has nothing to do with working code. If you remove columns from the database the project builds, since the project does not have information about the database structure. But as soon as you query for the table where the columns have been deleted, then the code should throw exceptions. I suggest you not to modify the database directly, but read into the topic of EF migrations so that modifying the model updates the database.

Changing schema name on runtime

I use Entity Framework 6 with an Oracle database in my project.
I have a problem: in my .edmx file (open in xml editor) all my triggers and tables has a name(schema) before the real table name or trigger etc, every time I install my project in new environment and database have a different name its try oldSchame.table and returns an error.
So for now I open .edmx in xml editor and delete all schemas name (remove schema.) and it works, but if I update my database schemas name back etc.
I have found many questions about this but all answers are old (e.g. from ~2010, and referring to Entity Framework 4); what is the best way to solve this problem for Entity Framework 6?

Making changes to existing entities in Entity Framework

Suppose I have an existing database set up using Entity Framework. Is there a mechanism through which I can safely add or remove entities (or their properties) such that the database is altered automatically?
I know there's an option to "Update Model From Database". Is there an equivalent "Update Database From Model" ? Is there a way to configure Visual Studio to do this automatically?
Entity Framework 4.3 has Code First Migration support.
EF helps you with checking the differences between your code and database and then generates code for you that handles this changes. You can use the NuGet package manager console to enable migrations, add a new migration and run them against your database (or create a sql script).
This blog explains how the Migrations work and this blog shows how you can use it with an existing database
Altering the database schema isn't a straightforward operation (has a column been renamed, or is it new column? Can the old type be converted to the new type?) that you can easily infer from the model.
EF doesn't alter the tables for you - it can Drop-Create the DB for you when you change it. However, if you change the existing database by hand to suit the model, EF doesn't seem to mind. It looks like what they check for is Hash(Model) = Hash(Tables).

EF 4.1 using wrong schema in generated sql

I am new to EF and i can't work out what I am doing wrong. I have used EF 4.1 "database first" to create a model for an existing database (that i can't change). All of the tables that i need in the database are in a particular schema which for this question i will call "my_schema". In the main properties of the edmx designer file i have set Database Schema Name to "my_schema". When i inspect the raw XML of the edmx file it seems to have the correct schema mappings
e.g.
<EntitySet Name="Events" EntityType="MyModel.Store.Events" store:Type="Tables" Schema="my_schema" />
However the SQL generated when i access the Events entity set on the DbContext class is still:
SELECT ....
FROM dbo.Events
I am not sure if it makes any difference but i am using the ADO.net DBContextGenerator to generate my classes.
Does anybody know what I am doing wrong.
OK i have sussed this now and it boils down to my ignorance of how EF works. I was passing my DbContext an ordinary ADO.net connection string which seems to flip it into code first mode. As such any settings and configuration in my edmx model were ignored and it was looking for attributes on the model classes. As soon as i changed it to use an EF string that includes references to the model metadata files it works. Seems obvious now, no idea how i expected it to magically know about model metadata.

Categories

Resources