EDMX has no relations present using the database-first approach in EF - c#

I am using Entity Framework to work against a database using the database-first approach, with an EDMX file representing my database. A problem happened when I changed one property in the database, then when I erased it from the EDMX file and updated it to add it again, it shows up now without the relations to the other tables (Pk-Fk relations).
More Info:
1. tried connecting from another new project and same result.
2. tried re-creating the database and re creating the pk-fk relation, and same result.
3. tried connecting to another database with edmx, and in this case the relations were present.
4. in the sql management studio it looks like the relations are present in the tables, as well as in the diagram.
Does anyone knows what is the problem?

When I need to update a table in the edmx it normally creates another .edmx.designer file, if this is happening this could be the reason why the relations doesn't appear. Just delete the older .edmx.designer and it should work again. If this isn't the problem, sorry but I'm new to entity framework, don't know it very well

Related

How do you edit a database migration?

I renamed a few tables and some columns. When I run the Add-Migration command, the migration generates code that drops the old tables and columns and adds ones with the new names. This results in losing the data they contained.
Since I don't want to lose the data, I want to edit the migration, removing the drop and add commands, and replacing them with rename commands.
But after I edit a migration, how do I apply that change?
If I run the Update-Database command, that applies it to the database. But not to the snapshot that Entity Framework maintains of my schema (stored in ApplicationDbContextModelSnapshot).
I need a way to incorporate my edits into the model. How can I accomplish this?
So, this is definitely the messy part of code first.
As far as the question asked, as GuruStron suggested, the only way I found to have a valid custom migration is to edit it such that the result is the same as what the original, generated migration produced. This keeps it up to date with the database snapshot. And running Update-Database will run your custom update code.
I think my biggest problem was that I had too many changes going on at once. After struggling with this for a while, I undid some of my changes and added them back bit-by-bit. Entity Framework will rename a table or column if it can figure out that the new name refers to the same column. If it finds many changes, it can't figure this out.
In the end, I had to customize the migration a little for a couple of columns that were being dropped (customized them to be renamed instead). But I was able to get Entity Framework to rename my tables and other columns.
The key: make small changes at a time and carefully review the migration before applying them to the database. If you need to customize the migration, do it such that the end result doesn't change.
You don't.
I suppose you are developing using a code first approach, since the question has this tag on it.
If you are using code first, you must change your models and let Entity Framework change the database schema for you.
Suggested reading:
Migration in Entity Framework Core
Entity Framework Core Migrations

entity framework with existing, non related database

I have an odd situation. I am working on a project with a very large existing database that is completely unrelated, but does contain corresponding table id's. It's as if someone copied the database but never related the tables.
In Entity Framework, is there a way to go EF code first and create the relationships in code, but Not apply those relationships in the database? I would like to go through and relate the database but the client doesn't want to pay to fix it.
Thanks!
In this instance, it seems you would be best to add relationships directly to your database (or to a duplicated database for testing/staging) and then just update your entities using your test connection and regression test your app.

How to add table in existing remote database and link it to Entity Framework model?

I tried finding the answer to this question before posting, but couldn't.
We are using a remote database with Entity Framework and I do not know the approach it was used to create the database. What I would like to do is add a class to the project, add the class to DbSet an create the table automatically. Now, when I use update-database in nuget, i get "migrations not enabled for this database" and its true, there is no configuration file or anything that suggest the approach it was used for creation of this.
I wouldn't want to enable-migrations as I don't want to mess something up or loss data. (or should i?) The existing tables are working fine with the repository...
I created a table manually, added a class that maps the properties by name and hoped that entity framework will pick it up, but no luck.
Here it is in a nutshell: I want to add a new table in a remote database that will be picked up by Entity Framework an generate a class for me. There is also NO .edmx file that can update the model. How was this done then... (?)
I am a new to Entity Framework, so apologies it some of this does not make sense. I am happy to clarify.
Thanks,
Thank you for your responses. I managed to fix this by adding a new class into the repository, using code first approach and then I created a table manually in the database.. The Entity Framework pick it up somehow :)
I was doing this before, but the problem was that I had the DbSet name to plural and database to singular. For instance: Products was the DbSet name and Product table in database. The actual class is still singular.
Thanks again!

Adding a new table to EF

We are upgrading an old VB6 application which sits on a SQL Server 2005 database, to an Entity Framework solution. The database remains the same, except - we're adding a new table. Is it possible with Entity Framework, to maintain the existing structure, when it gets installed on a client PC - and just add one new table?
Is this how Code First will work? Can I be 100% certain that no other tables will be modified?
i don't think, the effort is worth it to switch to code-first if you have an existing database and want to add only one table.
it is possible to map code-first classes to an existing database (reverse engineer code first). actually, i'm not very experienced with that workflow, but i know you can. You have to deal with a lot of manual mapping (with DataAnnotations or Fluent API), so in your case i would recommend to use the Entity Framework Database First workflow, since adding a single table saves you a lot of work.
this link has some useful information: Arthur Vickers Blog - Don't use Code first by mistake
You have two options, use a database editor such as SQL management studio to create the table which you can then map to a ef entity, or use migrations for ef which will let you update your database via ef.
Take a look at the migrations tutorial here: http://msdn.microsoft.com/en-gb/data/jj591621
I am using the database first approach, since a database developing team is doing the changes I require in the database on the SQL server for me.
Hence, I have to update the EDMX whenever the schema in the database changes.
Note: Changing one single table directly does not work for me, because VS doesn't always detect the changes right (for this issue, here are some details in SO if you're interested).
Hence, I am using the following workaround (regenerating all the tables):
In VS 2012, open the EDMX file by double-clicking on it. The graphic representation of the tables is shown.
Left-Click into the EDMX designer, then select all tables by pressing CTRL+A. Then, remove them by pressing DEL.
Right-Click into the EDMX designer and select "Update Model from Database ..." in the context menu.
The Update Wizard opens. In the "Add" tab, check "Tables", and depending on the requirements, check "Pluralize or singularize generated object names", "Include foreign key columns in the model" and optionally "Import selected stored procedures and functions into the entity model". Usually, I am using the "Pluralize..." and "Include foreign key columns..." options.
Click Finish. Now Save by pressing Ctrl+S.
That workaround works fine for me, and requires just a minute to update the model reliably.

How to get entity framework to realize the model and the DB are in sync

I am using Entity Framework code first for the first time in a production environment. Everything went fine until we got the DB up and had put some of the data in it and then to get some of the data we were importing from another location we had to change field lengths. So we made some of the fields nvarchar(99) instead of nvarchar(50).
That went fine and the application still worked but I knew I needed to change the data annotation or it would blow up later when it loaded and tried to save a too long field. When I did that the app blew up even though the model and the db are now matching. So I thought that it was the hash in the metadata table so I thought I'd be clever and make a new DB and take the hash from there and copy it. That did not work and in fact now I cannot get my app to connect to the test db that we have data loaded in at all.
I do not want to drop and recreate this database. I want entity framework to realize that the model and the schema do in fact match. Is there any way for me to do this? Also why did copying the metadata from a DB that entity framework created with this model not work?
Entity Framework Code First creates a EdmMetadata table and saves a hash of your Model classes in it. When you change something in the Model, the hash of the new Model classes doesn't match what's in the EdmMetadata table anymore, and the app should "blow up" at runtime. What you need to do to keep using the same database without dropping it, is to delete the EdmMetadata table. This way EF will not do that check and will try to proceed with the access to the DB.
Check this video tutorial (skip to 8:10 of the "When Classes Change" section).
Sorry I fixed this. Removing the metadata worked. But turns out I had updated to a more recent version of EntityFramework accidentally while trying to fix my problem and this more recent version expected different naming conventions for the Database. In any case recreating the many-to-many group person table with a script from a DB created by Entity Framework and deleting the metadata fixed the problem.

Categories