What causes "O/R Designer validation failed" after minor changes to a .dbml file using Designer? - visual-studio-2010

I added some columns to a table using SQL Server Management Studio, and then wanted to reference them in my project in Visual Studio 2010, where it is loaded using Entity Frameworks, which seems not to have a handy feature for simply importing updates from the database.
So I manually added the columns. They were just bit columns like an existing column, so I used cut and paste to duplicate that column, then renamed them and filled in the Storage field to match.
But then when trying to build the project, it would fail with:
O/R Designer validation failed . . . myDB.dbml Error: The operation could not be completed. Unspecified error
The main tips I found about these not-very-helpful error messages, involved restarting Visual Studio. That didn't help me here.
What else can cause (and what can fix) this error?

What caused this in my case, was an apparent bug in Visual Studio 2010's visual DBML editor!
That is, the new columns I had added and manually entered correctly, did not get saved correctly. This was somehow invisible even when closing Visual Studio and reloading the editor - there, it appeared correctly.
However, when I looked at the actual .dbml file using Notepad++, I could see that the Storage= values were all still the same as the column I had Cut & Paste from, rather than the correct values I had hand-entered, and that showed in the visual DBML editor!
I notice that the correct columns do not even have a Storage= field, which is probably a clue to what the bug in the editor is.
I deleted the incorrect Storage= fields from the .dbml file using Notepad++, and this solved the build problem.
I also needed to go into the visual designer and just make a random trivial change (I nudged the position of the table in the display) before it would add member variables for the columns I had added, to the auto-generated LINQ .cs file.

Related

L2S DBML Table Stuck as "Internal" Visual Studio

I have a Linq to SQL data context with several tables. One particular table is marked in the DBML designer as having Public access, however when I save and compile the assembly it always changes the access modifier in the .designer file to internal. Thus the build always fails (as I have classes in other assemblies that reference this table) and I have to go into the .designer file and change the internal modifier back to public. Any change to the DBML file at all causes this to happen, even if I specifically set the property in the designer (toggle it back and forth, whatever). Is this a bug in VS (problem experienced in VS2010 and VS2013) and is there a fix?
Thanks.
I believe I have finally found a solution:
Open the .dbml file outside Visual Studio (e.g. Notepad)
Locate the problematic table definition.
Change AccessModifier="Internal" to AccessModifier="Public"
Save and close the .dbml file
Update the table definitions in the .designer.cs file with the public modifier if you have not already done so.
In theory the dbml designer should be doing this for me when I change that property on the table but unfortunately it doesn't for these particular tables. Following these steps fixed the problem.

Visual Studio Converted Project User Control Designer File Incorrect

I have an ASP.Net project that I converted to C# from VB using InstantC#. The conversion appears to have happened pretty well. Only a few items had to be fixed manually. I did this after converting the project from VS2010 to VS2012. Once I fixed the inferred method and object references, I was able to build once.
Upon viewing the page, I received an Object Null Reference Exception on an event handler assignment.
I built again. This time, I received all kinds of errors from the methods and properties for code that referred to other user controls that the page has on it. In looking at the designer file, it is not correctly determining and assigning the types to the controls.
I have tried manually changing the object types, but Visual Studio changes them back.
I have also tried deleting the designer file and choosing Convert to Web Application.
There are other user controls in the same project that work fine. No issues. Any other ideas on how to fix this?
I finally fixed this by creating a brand new user control, and then pasting the UI from the original user control. Once I confirmed the designer file was generated correctly, I imported the code behind from the original user control.
To be sure everything went as planned, I renamed and excluded the original user control from the project and tested the new one. When I confirmed that things were working, I deleted the original.

Entity Framework + Team Foundation Server - edmx error

When I try to update the existing edmx Model while connected to TFS i receive an error:
"XmlModels involved in this transasction are not editable".
What's even worse, is that when I try to generate a new model, Visual Studio just crashes out.
It happens only when I'm working online on a project, being connected to team foundation server.
So far the only solution I've found was to edit .edmx file using text editor...
Thanks in advance for any responses.
From: http://connect.microsoft.com/VisualStudio/feedback/details/299134/vs2008-beta2-ado-net-entity-data-model-and-wpf-app-issues
Please make a copy of your edmx file before you attempt this action.!
For ADO.NET: Duplicate InheritanceConnectors and/or AssociationConnector seems to be the problem, so deleting everything in the edmx:Diagram tag except entitytypeshape fixes the problem when you save and open again.
Other people indicate that closing and re-opening the solution or rebinding to the source control system completely could have a positive effect.

How do I recover an Entity Framework 4 EDMX file when Visual Studio 2010 will not open it?

I have been following the examples from Chapter 2 in an early access edition of Entity Framework 4 in Action whereby one creates a database schema, generates an entity model from the database schema, and then performs some entity customizations with regards to object inheritance, adding complex properties, etc. Everything was great until I closed and then restarted Visual Studio 2010 SP1. After restarting, I can no longer open the EDMX file with the designer. I get a dialog box stating "Cannot load 'C:\Projects\Test\Test.MyModel\Model.edmx': An item with the same key has already been added.". There is no further information in the Output window.
How might one might recover the file?
How might one debug the model loading process to find a little more information on what "item" is attempting to load or even what type of "item" it is?
It's an XML file so you can open it up in text mode and see what the problem is. Sounds like there could be items with duplicate names somehow.

How do I update Linq intellisense when I alter my table? [duplicate]

How do I update a Linq to SQL .dbml file?
There are three ways to keep the model in sync.
Delete the modified tables from the designer, and drag them back onto the designer surface from the Database Explorer. I have found that, for this to work reliably, you have to:
a. Refresh the database schema in the Database Explorer (right-click, refresh)
b. Save the designer after deleting the tables
c. Save again after dragging the tables back.
Note though that if you have modified any properties (for instance, turning off the child property of an association), this will obviously lose those modifications — you'll have to make them again.
Use SQLMetal to regenerate the schema from your database. I have seen a number of blog posts that show how to script this.
Make changes directly in the Properties pane of the DBML. This works for simple changes, like allowing nulls on a field.
The DBML designer is not installed by default in Visual Studio 2015, 2017 or 2019. You will have to close VS, start the VS installer and modify your installation. The LINQ to SQL tools is the feature you must install. For VS 2017/2019, you can find it under Individual Components > Code Tools.
To update a table in your .dbml-diagram with, for example, added columns, do this:
Update your SQL Server Explorer window.
Drag the "new" version of your table into the .dbml-diagram (report1 in the picture below).
Mark the added columns in the new version of the table, press Ctrl+C to copy the added columns.
Click the "old" version of your table and press Ctrl+V to paste the added columns into the already present version of the table.
Delete the dragged table from step 2 and save the .dbml file.
You could also check out the PLINQO set of code generation templates, based on CodeSmith, which allow you to do a lot of neat things for and with Linq-to-SQL:
generate one file per class (instead of a single, huge file)
update your model as needed
many more features
Check out the PLINQO site at http://www.plinqo.com and have a look at the intro videos.
The second tool I know of are the Huagati DBML/EDMX tools, which allow update of DBML (Linq-to-SQL) and EDMX (Entity Framework) mapping files, and more (like naming conventions etc.).
Marc
We use a custom written T4 template that dynamically queries the information_schema model for each table in all of our .DBML files, and then overwrites parts of the .DBML file with fresh schema info from the database. I highly recommend implementing a solution like this - it has saved me oodles of time, and unlike deleting and re-adding your tables to your model you get to keep your associations. With this solution, you'll get compile-time errors when your schema changes. You want to make sure that you're using a version control system though, because diffing is really handy. This is a great solution that works well if you're developing with a DB schema first approach. Of course, I can't share my company's code so you're on your own for writing this yourself. But if you know some Linq-to-XML and can go to school on this project, you can get to where you want to be.
I would recommend using the visual designer built into VS2008, as updating the dbml also updates the code that is generated for you. Modifying the dbml outside of the visual designer would result in the underlying code being out of sync.
There is a nuance to updating tables then updating the DBML... Foreign key relationships are not immediately always brought over if changes are made to existing tables. The work around is to do a build of the project and then re-add the tables again. I reported this to MS and its being fixed for VS2010.
DBML display does not show new foreign key constraints
Note that the instructions given in the main answer are not clear. To update the table
Open up the dbml design surface
Select all tables with Right->Click->Select All or CTRLa
CTRLx (Cut)
CTRLv (Paste)
Save and rebuild solution.
In the case of stored procedure update, you should delete it from the .dbml file and reinsert it again. But if the stored procedure have two paths (ex: if something; display some columns; else display some other columns), make sure the two paths have the same columns aliases!!! Otherwise only the first path columns will exist.
Here is the complete step-by-step method that worked for me in order to update the LINQ to SQL dbml and associated files to include a new column that I added to one of the database tables.
You need to make the changes to your design surface as suggested by other above; however, you need to do some extra steps. These are the complete steps:
Drag your updated table from Server Explorer onto the design surface
Copy the new column from this "new" table to the "old" table (see M463 answer for details on this step)
Delete the "new" table that you just dragged over
Click and highlight the stored procedure, then delete it
Drag the new stored procedure and drop into place.
Delete the .designer.vb file in the code-behind of the .dbml
(if you do not delete this, your code-behind containing the schema will
not update even if you rebuild and the new table field will not be included)
Clean and Rebuild the solution (this will rebuild the .designer.vb file to include all the new changes!).
Use Visual Studio 2019 open DBML files in graphic designer to update DBML files.
when not working:https://developercommunity.visualstudio.com/t/dbml-files-not-opening-in-graphic-designer-in-vs-2/1659675
Just for the record

Resources