Entity Framework 6.1 delete a column - c#

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)

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.

Entity Framework Code first rename models/tables in Database

I need to upload a project from Visual Studio to Azure, but I have to share the same DB with other projects. So I guess I need to rename all my models that creates the tables in the DB to prevent any conflict with other projetcs model/tables!?
I started to change the name of one the model and then continued to change in the code and also in IdentityModels.cs file, but will this really work? Do I need to change in other files also? One way to simplify it all, would be to use search and replace, but then I have no control of what is changed!
Any good advice how to replace name of table in DB and all instances?
i think there is a migration plan if you change your model after creating the data base
link :
https://msdn.microsoft.com/en-us/data/jj591621.aspx

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

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.

EF Database first how to update model for database changes?

In a class library Ado.net Entity Data Model is has generated POCO classes. These were generated fine for the first time. But database changes are not being reflected. In edmx diagram right clicking and choosing Update Model from Database show newly created table but it do not add table even after selecting it to add.
I tried running .tt (by right click and Run custom tool) but even it did not regenerated the Poco classes as per latest DB changes.
Help please
Not a fix but a workaround: Is it not an option to simply remove and regenerate the EDMX and the generated classes? That's what I do, it is much easier than working with the update feature, and the result seems to be the same. Your POCO extensions still remain the same and functional.
I use database first and I have my SQL upgrade scripts, the generated EDMX and my Generated models in source control and the changes there are very easy to manage. Here is a rough outline of my DB upgrade process for each version:
Create .sql script for the upgrade, statements like CREATE TABLE etc.
Delete generated files: Model.Context.tt, Model.tt, Model.edmx
Remove Entities string from Web.config (if you use it)
Create the EDMX and Context files the same way you did for the first time
If you use source control (I hope you do!) check what has changed
Test
Commit!
In my case i needed to save ModelName.edmx, then classes were generated.
Ensure that connections string in app.config is correct. I was using a DataDictionary and my connection string had the following path:
data source=|DataDirectory|*.sqlite
Thus, it wasn't updating. Because this DataDirectory variable was being resolved at runtime.

Categories

Resources