System.Data.SQLite.EF6 Entities not found after modifying edmx diagram - c#

I'm using EF6 with System.Data.SQLite 3rd party provider (there is no native support for SQLite in EF6).
I have to add inheritance between two tables. Because I can't do this on database level, I have to modify the .edmx diagram:
remove the original connection between the two tables (1:N connection), and replace it with the inharitance
rename one of the two tables, because EF automatically generates the name, which is not fit to our naming conventions
After I do this Visual Studio won't see the tables (none of them, nor the tables I have not modified), and I get several errors after build ("The type or namespace '' could not be found." ). Also, all of my table.cs files get removed from under ApplicationDatabase.tt.
If I undo the changes (revert back to the 1:N connection), and save the diagram and the database model everything works fine again.
What am I missing during the changes?

Ok, I solved it by removing the Id from the inherited table. I think the problem was that both tables had an Id, and it conflicted somehow because of the inheritance. Now everything just works fine.

Related

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.

Entity Framework 6.4 Duplicating database name on tables ex: DatabaseName.DatabaseName.TableName

Entity Framework is duplicating database name on tables ex: DatabaseName.DatabaseName.TableName. My connection string config and web.config do not have duplication in them. Has anyone seen this before?
As you see from the error the table is:
MySqlException: Table 'nsf_erc_db.nsf_erc_db.personnel' doesn't exist
When it should be table nsf_erc_db.personnel
Whenever I've had issues with Entity Framework, I'll sometimes delete and re-add everything to the model. Goes without saying, but just to be safe, make sure you got a version of your project you can role back to.
To do this, select all of your tables in your model -> right click any of the tables -> select Delete from model.
Once everything is gone, right click in the empty model space and select Update model from database... and then re-add all your tables from the Add tab.
If doing that still doesn't work, you might have to manually go into the .edmx file and do some changes yourself. Messing around in the Model Browser could work too.

How to avoid program exception when ef model changes?

I use a third party database with Entity Framework 6. This works fine; however, when a table within my model is changed (three columns were deleted), my program throws an exception:
System.Data.SqlClient.SqlException: Invalid column name '<deleted column>'
I don't use any of these columns. I only read them from the database.
I can update my model, but then when there is another change in a table, my program will crash again. How can I modify my program so that it won't crash on the next database change?
You can use a Code First approach starting from database (generate classes from database). At the end of class generation you can delete entities that you don't need (i.e. entities related to all unused tables) or properties related to unused fields.
Disable migrations.
You can also delete intermediate files generated by EF code generation (files different from .cs files).
At this point, any changes to database that not affects mapped classes/properties does not cause errors in EF.
Create a context with only the entities you need. Create entities with only the properties you need.
see EF code first.
Use Fluent API to specify primary keys and more.
It will still crash though if any of your entities/properties gets changed/deleted

Entity Framework 6.1 delete a column

I am working on an application with Entity Framework (current version 6.1.1) Database-first and Sql Express 2008 as a storage. I have a very simple model that consists of two tables Foo and FooElements with one-to-many relations.
I have deleted column Bla from table Foo, opened my .edmx file, right-clicked an empty space and selected "Update model from the database", but the deleted field remained in the model.
When I try getting the Foo entities from the DB, an exception is thrown: Invalid column name Bla. So I deleted the whole Foo entity from the model, added it from the DB again, now I have another exception - property Bla does not exist.
I tried restarting Visual Studio, removing obj and bin files, removing the data model from the project completely and creating it from scratch - it requires property Bla in the model, but fails when I create it manually (column Bla is not created automatically hence the column does not exist in the DB).
The only solution to make my project running again is to create column Bla in the DB again and leave it there, despite the fact that I don't need it.
Is there any way to delete a column from a DB so that the Entity framework can work afterwards?
Is there any kind of cache file that I have to clean up?
Thank you.
P.S. Even thought the field does not exist in the edmx file, Entity framework still thinks it is required. Where can it get an idea, that there are still certain fields in the DB?
Thank you all for your ideas.
The problem was even more trivial than I expected. Another project in my sandbox solution had Entity Framework referencing the same table in the DB and it had the same model name, and since I completely forgot about it, I did not update it, and the .edmx file in the second project contained the old deleted fields. It never came to my mind that two different assemblies could interfere like that, but they did.
The solution is to remove (or rename) the model in the second project and it is working now.
This happened to me recently.
Try manually running an Update-database with -force (Do this even if your're using Automatic Migrations):
PUT BACK Bla column manually to the DB, ALSO REMOVE the Bla field from your model and run:
Update-database -verbose -force -ConfigurationTypeName Configuration
You can Put instead of "Confiuration" your configuration name.
In some cases, like when there are multiple configurations, you'd have to specify the configuration full path like this:
AppName.MigrationsNameSpace.ConfigurationName
(most time would simply be "Configuration" where the "ConfigurationName" is)

Entity Framework conflict with same table name from different databases

I am using Entity Framework 4 with MVC 3 in Visual Studio 2012 (C#).
I am using database first; there are two separate databases each with its own namespace and with two separate edmx files. Each database has a table with the same name and fields (but different content). When I added the second table I started to get compile errors.
Ambiguity between 'Interface.CodeFormStatus.FormStatusCodeID'
and 'Interface.CodeFormStatus.FormStatusCodeID'
There seem to be some complex workarounds or I could rename one of the tables. Is there not a straightforward solution as this must be a fairly common issue.
I ran into a situation where I had two databases (one an older version of the other) and I needed to integrate both into a single project. Naturally, almost every name conflicted.
I created two separated edmx files for each database, and put each in its own namespace for clarity. I then edited each entity name to reflect which database it was coming from - (e.g. "Activities", which was in both, became "v13Activities" and "v14Activities").
For operations which were to be mirrored between both databases, I wrote a wrapper that included both contexts. This made my code was much less repetitive, and it had less synchronization issues.
Hope this approach helps someone else - it seems like this is an obscure question, and this answer was one of the top results on Google!
Update: In EF 6.1+, there is another solution. You can have "conflicting" names, and separate them with simple namespacing when using the "Code First From Database" option. I would advocate for this solution going forward, as the old XML .edmx style is going to be phased out starting in EF Core.
This worked for me. Just click on the table in the designer (the graphical version not the code) Then in the properties next to the, "Name" attribute you can change the name to something different. This will just change the name within the designer and used more as an alias throughout the application.
If you don't have many tables with the same name, then you could edit entity name in designer (your .edmx file).
So, just double-click a name of one of your CodeFormStatus entities and make it different (for example, change it to CodeFormStatusOther)

Categories

Resources