Entity Framework 6 Update from Database ignores deleted columns, type changes - sql

Am I missing something here?
I've a .net 4.5.1 project with an Entity Framework model created from a SQL 2005 database (Connection type is SQL Server also), via "ADO.NET Entity Data Model".
This works fine. That is, until I update the database. If I add new tables or columns to existing tables all is well, but if I delete a column, the update just doesn't work properly. it also throws out errors when I alter the column type.
This same problem was reported a long time ago: http://blog.jongallant.com/2012/08/entity-framework-manual-update.html#.UytNrvldVD0, but it seems so ridiculous I can't believe I am not missing something. Surely I'm doing something wrong? How can I get the model to update properly?

I have the same problem. I opened up the Model, removed the changed tables from the diagram, then Updated from the Database and re-added the tables.
EF brought the table back in again with the correct structure. Save The model, Rebuild the project, everything turns up as expected.
I am not sure why it doesn't detect the deleted columns. I believe there's also issues if you change the data type of the columns, but I've not tested it. But the above solution has worked for me so far.

Related

Problem: Adding a column to MS Access DB that is used by a VB.NET program via OLE no longer updates the automatically generaate code

I have a VB.NET program that I wrote 20 years ago and last updated 7 years ago. I have gotten a new computer and new version of Visual Studio recently.
The program uses a DB created with MS Access via the OLEDB mechanism. I originally created the DB connection by dragging and dropping tables from the DB into a VB window. This generates a lot of code automagically that I have never had to look at in the past. This code takes care of the DB queries and updates. Code I have explicitly written adds, modifies or deletes rows from the tables.
In the past I have been able to manually add a column to the Access DB and get the automatically generated code to update and make that column available to my code.
I am trying to do that now and I've either forgotten a crucial step or the newest version of Visual Studio is not compatible with my ancient code. The symptom is the new column does not get updated when I update all the other columns. If I look at the automagically generated code (in routine InitilizeComponent) it has lines like:
Me.OleDbUpdateCommand5.CommandText = resources.GetString("OleDbUpdateCommand5.CommandText")
Me.OleDbUpdateCommand5.Connection = Me.dcWageLossDB
Me.OleDbUpdateCommand5.Parameters.AddRange(New System.Data.OleDb.OleDbParameter() {New System.Data.OleDb.OleDbParameter("CaseId", System...
where in the arguments of the last call it lists all the columns EXCEPT for my my new one.
Note that while the new column does not get properly added to the DB queries, it does get added to internal structures so that in my code I can refer to the new column by name and set its value.
I know this question is rather fuzzy. It is not practical to provide the (quite long and complex) code. Since the problem probably depends on the long history, it also is not practical to try to create a small simple version of it.
I'm just hoping someone has run into this problem and knows the solution.
Thanks
I have already tried graphically deleting and adding back all the tables in the .xsd file used in the query via the solution explorer - data connections. It didn't help.
In desperation, I also tried modifying my hand the automagically generated code, adding it the new column in a fashion similar to what it had for all the old columns. I couldn't make that work.

Fixing a broken database .NET

Okay so I am working in Web Forms but the problem would also apply to MVC I am assuming since both have the option of creating a users database on project creation. I deleted a data table on accident and updated the database instead of deleting the database itself because I was trying to recreate it with the seed data. I didn't realize that deleting a data table would do something different from deleting the database itself. The only backup I have is pretty old, so I would prefer to use a different way to fix things if that exists. How would I fix things?
I have it working now. What I did to fix it was I went to my back up although it could have just been a brand new project, both would have been fine, and I copied the SQL script for the data table that I deleted. Then I went to my broken program and created a new data table in the spot where it was before and replaced the code with the code from the back up. I saved it and hit update and it updated the database for me.

Peculiar happenings in SQL Server

I have just started an internship in I've had to learn a lot on my own. I'm learning MS SQL Server, but having a strange problem. I have a DB that has four small tables. Each one has a script to drop the table, recreate the table (I've avoided FK dependencies for the time being), and execute a demo query.
Problem 1: When I first started SQL Server Managmenent Studio would execute the script, but one table didn't show up in the Object Explorer. If I tried to execute a demo query from the same .sql file, it executed with no problem. If I tried to access it from another .sql, the table didn't exist. After many times of successfully executing the script, it finally just showed up.
Problem 2: Similar problem. When I updated one table, the changes wouldn't be reflected in queries.
Problem 3: Queries will fail, but if I click execute again with no changes being made, it will usually work correctly.
Problem 4: When I use an alias for a field name, sometimes the alias is recognized and sometimes it isn't. I've literally had single query in which the alias would work in one place, but not work in another and I had to use a fully qualified name.
I've tried the refresh and refresh local cache, but those seem to have no effect. If I exit Management Studio, that seems to usually fix the first two problems.
Am I going nuts or am I just in the dark about some weird specifics of SQL Server?
First of all, when you make a schema change you need to right-click on the "Tables" node for the database in management studio and hit refresh.
If you change a column or something in a table, right-click the table and refresh.
The refresh local cache only updates the intellisense stuff, and the refresh only updates the GUI. If you modify a table with SQL and do not refresh it in the UI, the query can still use the updated table.
If you query fails, you either have a bad query or it's not pointed at the database or connection you think it is.
For aliases, there are places where they will not work (update statements, for example) but if you don't post queries where they don't work we cannot read your mind and tell you what's wrong.
If you have specific queries that are failing, post them.

Use SQL Server Management Studio to update code first project

I have created a project and added an Entity Data model using Code First from Existing Database.
Now, I like using SSMS (2008) to make changes like adding new columns, tables or changing some of the properties table or column properties.
Is there a way that I can make my changes in SSMS then have the Models and Migrations updated accordingly or once you have selected Code first and created you models are you stuck to only making changes in code and running the add migrations and update database from the package manager?
I also need to make sure when I publish the solution the server version of the database is updated accordingly.
Cheers in advance,
Kevin.

FluentNHibernate: Getting the Examples.FirstProject to work

Im trying to get the most basic of examples to run in FnH. I started with the Examples.FirstProject. However, I did not use the SQL lite configuration. Instead, I set the configuration to SQL2005 and created the tables as was diagramed in the example.
When stepping through the code, there appears to be no problems when creating the session factory. I do receive an error however when the code reaches the "transaction.commit" line. The error reads:
Could not insert collection: [Examples.FirstProject.Entities.Store.Products#5][SQL:SQL not available]
Im wondering if there was an issue with the way the tables were created in SQL Server. The IDs were "int" type and the names and such were "varchar(50)." I set the PK of Store, Product, and Employee to its respective ID field. I also made the ID increment automatically by 1 (IdentitySpecification column property in SQL Server). StoreProduct is the many-to-many and is also there per the diagram.
Any help would be appreciated. Thanks.
Have you modified the sample in any way other than changing the database provide? Have you been able to save any entities from the sample (ie if you remove the Products code and just save the Store)?
I developed this sample against SQLExpress, so I would imagine there wouldn't be any incompatibilities with SQL 2005.
Also, this question would probably be better be suited to the Fluent NHibernate mailing list, as Stack Overflow isn't great for these kind-of investigatory postings.
Thank you James. I'll look at using Fluent NHibernate mailing list. As a solution to my issue, I did simplify the example a bit and found that rebuilding the tables helped. In the previous attempt I built the tables in the Database diagram tool. That is where I think something was a little off. Just now I rebuilt them using the menus and still made the foreign key connections with the Database Diagram section. Worked like a charm. Thanks again and keep up the good work with FnH.
First thing to check: Are you sure that you have really created a correct table in SQL server, and that the schema is correct? You can verify this by using SQL Express management studio to view the sql database.