need to remove a column from a table in a Linq project - c#

I have extensive experience working in ASP.NET and Sql Server but I am new to Linq. I have just inherited a project that was created using Linq.
Unfortunately the last developer knew nothing of efficiency and was storing images in the database in a truly terrible way. I have modified the code so that it no longer uses the column that stored the image. Now I want to completely delete that column from the database to keep the Linq queries from wasting time and resources pulling in these huge files.
I searched my project for every reference to the column and removed it, then deleted the column from the database (don't worry, I have plenty of backups of everything). When I did this I began to get error messages about an invalid column name for the column I deleted.
So my question is, how the heck do you modify the structure of a table when using Linq?

You need to be sure to remove the column from the DBML itself. Just view the DBML in the designer and delete the appropriate column. You would not get any error at compile time since it does not check to see if the DBML actually matches up with the database during compiliation.

Just delete the table from the Linq-Sql designer, then add it again.

If you removed the file from the dbml file, and it didn't pick up on the change, go ahead and right-click the dbml file and choose "Run Custom Tool". Note that if you look at the file's properties (right-click on the entry in solution explorer) you should see the custom tool listed as "MSLinqToSQLGenerator".
Worst case: If you expand the file, and look at the "dbmlfilename.designer.cs" file you should be able to find the field/column name in question. Go ahead and delete it from that file. (one property (with attribute and getter/setter) and one field with the same name (starting with an _ character).
It's the attribute on the property in the designer file that causes the runtime exception.

Related

Unable to get the added fields of Database after making changes

I am using Linq to SQL approach to do manipulation with the database tables. I am stuck with the one problem, whenever I am adding a new column to my existing tables then I have to delete existing .dbml file of the table from the Solution and then have to add again to get the access of last added columns.
I tried the approach by re-building Project and the Solution as well but still, It doesn't work and pushes me to do the above less-sensible steps.
Could you help me out to get rid of this issue? or It is the only way I have to get the changes by adding .dbml file again.

Problem with Entity Framework after adding column to DB table

I needed to add a new NVARCHAR column to a table in my DB. So I added the column, and then fired up Visual Studio to update the EDMX-file for the Entity Framework.
I ran update model from database on everything, which only resulted in "data reader is incompatible"-errors. So I renamed the whole table in the DB, updated EDMX from database, renamed the table back to the original name, ran update again, and then created new function imports for all affected stored procedures. But I still get the same error:
The data reader is incompatible with
the specified '[Model].[Entity]'. A
member of the type,
'[Column]', does not have a
corresponding column in the data
reader with the same name.
I've looked around a bit, and this seems to be a common error if the column name is different in the database and framework. This is however not the case, they have the same name.
I can access the column in the code via [Entity].Context.[Column], so I don't quite see what the data reader is complaining about.
I've run out of ideas, so any help welcome.
Updating the model replaces the store schema, but not the client schema or the mapping. To start with "a clean slate", back up your current EDMX, then open it as XML. Remove all references to the table, then close and open in the graphical error. Build. If you have any errors (perhaps broken links to the deleted table), fix them. Then update model to re-add the table.
Just had the same issues as above. Tried removing the entity from the edmx, tried removing and re-adding the function import, and finally rebuilt using a complex type in the function import and that didn't work either.
I realize this error can occur for really any scenario where there is a mismatch but we found the issue was in the stored proc we used for the function import. The stored proc was using specific select columnname, columnname etc and the new column we added to the table was not in that list. To test, we used the *, updated the EDMX and it resolved our issue.
Turns out the EDMX was fine, but the designer has, for some odd reason, stopped update Designer.cs in my project.
Had to go in and manually edit it.
For me it was an issue where I altered an entity (vertical entity splitting), but one of my stored procedures wasn't pulling data for that new field. I added it (in the SP) and now everything runs fine.
Unless it's simply adding a new item, This throws an error just about every time I try to "Update model from database" ...
The solution is luckily very simple -
1) Open Web.config, find <connectionStrings> node
2) Delete the DBNameEntities <connectionStrings>
- this way you do not have to modify any references to your emdx model
2) Delete the ADO.NET Entity Data Model (.emdx)
3) Re-add the ADO.NET Entity Data Model (.emdx) with the same name.
Its faster and bulletproof. (so far!?)
If you Re-Add the DataModel you will lose all DataBindings - especially bindings of controls on your form (like DbGrid). I have fixed this issue manually editing the DataModel.edmx file in external Editor.

One or more files do not match the primary file of the database (error 5173)

I got this error when I checked out my database from source control. It might sounds weird to check in the sql server database, but this was what I have done because this is just a personal project.
Anyone knows how to fix this?
Here's my finding.
As mentioned by other posters, you really don't want to check database files into and out of the source control.
But if you absolutely need to, and you have done check in the database files and you are encountering the same error that I encountered, here is a workaround:
First, detach the database, then, delete the ldf file, reattach the database again.
This is how I solved my problem.
Did you take a copy of the log file (.ldf) as well as the ".mdf" file? You need the matching set of both to re-attach the database
You really don't want to be checking database files into and out of source control - in SQL Server you have to detach the files for this to even work and you run all kinds of risks.
If you absolutely have to do this, you should version backups.
I recommend versioning a script which creates the entire database (tables, sprocs, views, etc.)
You can try creating a database attaching from that data file and using Create Database the ATTACH_REBUILD_LOG option, but I'm not confident it's going to work since they probably weren't detached properly.
This sounds like the data files do not match the structure files of your database.
Shortly spoken, the files where your data (i.e. table rows) resides in are (mostly) not the files the structure of your data (i.e. the description of the tables) is stored. At least in "modern" RDBMS systems. So you checked out your data and the database recognized some changes in the structure which happened till then (you altered a table or something like that).
The way "to fix this" would be to check in all files your database relies on, but I think that is not really what you wanted to achieve. Better (as mentioned above) to do backups and then drop / restore the database from them.

Best way to update LINQ to SQL classes after database schema change

I'm using LINQ to SQL classes in a project where the database design is still in a bit of flux.
Is there an easy way of synchronising the classes with the schema, or do I need to manually update the classes if a table design changes?
You can use SQLMetal.exe to generate your dbml and or cs/vb file. Use a pre-build script to start it and target the directory where your datacontext project belongs.
C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\x64\sqlmetal.exe
/server:<SERVER>
/database:<database>
/code:"path\Solution\DataContextProject\dbContext.cs"
/language:csharp
/namespace:<your namespace>
I haven't tried it myself, but Huagati DBML/EDMX Tools is recommended by other people.
Huagati DBML/EDMX Tools is an add-in
for Visual Studio that adds
functionality to the Linq2SQL/DBML
diagram designer in Visual Studio
2008, and to the ADO.NET Entity
Framework designer in Visual Studio
2008 SP1. The add-in adds new menu
options for updating Linq2SQL designer
diagrams with database changes, for
renaming Linq-to-SQL (DBML) and EF
(EDMX) classes and properties to use
.net naming conventions, and for
adding documentation/descriptions to
Linq-to-SQL generated classes from the
database properties.
Here is an easy fix without any additional software, that just works for simple changes (like added fields, few tables, etc).
Instructions:
You pull a copy of the changed table into the designer (will be removed later)
Now select all the new (or changed) fields and (right-click ->) copy
In your original table right click and insert them (delete changed fields first)
Now delete the table you copied them from
I know it is kinda obvious, but somehow non-intuitive, and it helped me a lot, since all the right attributes and types will be copied, and all links stay intact. Hope it helps.
When to use:
Of course it is - as said - for small changes, but surely better than manually replacing tables with many links, or when you don't want your whole database structure generated by SQLMetal. For example when you have a big amount of tables (e.g. SAP), or when using cross-linked tables from different databases.
DamienG has written some t4 templates which can replace some of what VS generates for you. These can be rerun whenever you like via a command line tool.
T4 templates have the added benefit of being editable. This allows you to tweak what is generated to you hearts content.
I think Jeff complained about this recently. One common technique is to drag all the objects into the designer again...
I hope someone else chimes in with a better approach!
I wrote a tool to do script changes to Dbml scripts see http://code.google.com/p/linqtodbmlrunner/ and my blog http://www.adverseconditionals.com
How about modifying the Properties of the entity/table within the DataContext design surface within Visual Studio?
For instance if I added a column to an SQL Server table:
Open the *.dbml file.
Right click the entity and select Add > Property.
Fill out the values in the Properties window for the new column.
Build your solution.
The auto generated model classes should reflect the new column that was added.

How can you easily reorder columns in LINQ to SQL designer?

When designing LINQ classes using the LINQ to SQL designer I've sometimes needed to reorder the classes for the purposes of having the resultant columns in a DataGridView appear in a different order. Unfortunately this seems to be exceedingly difficult; you need to cut and paste properties about, or delete them and re-insert them manually.
I know you can reorder columns fairly easily in a DataGridView, however that would result in a lot of hardcoding and I want the designer to match up to the grid.
Does anyone know of any easier way of achieving this or is cutting/pasting the only available method?
I tried manually editing the .designer.cs file, but reordering properties there doesn't appear to do anything!
Edit: Just to make it clear - I want to reorder what's in the LINQ to SQL designer, not what's in the table. I haven't made an error in ordering requiring a reversion to the original table layout; rather I have a table which I want to possess a different ordering in Visual Studio than in SQL Server.
Open the [DataClasses].dbml file in your favorite XML editor, and reorder the [Column] elements for the table. Save, and reopen (or reload) the designer in Visual studio. The order of the columns displayed in the designer will be fixed.
Using Linq-to-Sql, you can have columns in the DataGridView appear different than in the original table by:
In your Linq query, extract the columns that you want, in the order than you want, and store them in a var. Then the autogenerate columns should show them in that order in the DataGridView
Use Template columns in your DataGridView
Do not use drag-and-drop on the Linq-to-Sql design surface to create your entities. Rather, create them by hand and associate them with the database table using table and column properties
As far as I know, there is no drag-and-drop column reorder in the designer itself
If you are in the scenario where you have reordered the columns in the database, and you now want to have this new order be reflected in the designer, I think that you have to delete the table from the designer and then put it in again. Or if you use SqlMetal to generate your Linq-to-Sql classes, rerun it on your database and use the newly generated file.

Categories