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.
Related
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.
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
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.
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)
I am trying to create an Entity Model from a database. When I add the tables EF does not read my existing FK constraints. If I make a Linq to SQL file in the same project and add the same tables it reads the constraints without issue.
Whats the deal?
Recently I had the same issue - I am not sure why, but somehow the Entity Framework conflicts with some non-clustered indexes.
Similar situation:
http://blog.degree.no/2012/09/missing-relations-in-entityframework-model-when-generating-from-database/
You can try removing your indexes, regenerate your model, add the indexes again and update the regenerated model - it worked for me.