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.
Related
I am migrating an existing application that uses ADO.Net to .Net 4+ with Linq2SQL. The problem is that the application's database contains a table that is customized at runtime using a GUI designer, i.e. the table initially contains the primary key (Id) and 3 other columns, but then the user designs the data that will be tracked in the application using a GUI designer which in turn modifies the structure of this table adding the columns to accommodate the need. The designer also records the structure of this table in a meta-data table that describes these custom columns.
The current application builds SQL strings to build queries. How can I handle querying from this table using Linq2SQL? Is using context.ExecuteQuery<> the only way to do it?
I believe you can use dynamic.cs to get what you are looking for. Might be better to use stored procedures and have the dynamic sql done in the sprocs. Here is the link to dynamic.cs with examples.
dynamic.cs
I've got an ASP.NET project, and the customer would like a way to easily change the recursive parent-child relationships between certain items in a table. For example, if Entity2 is a child of Entity1, and the user wants Entity1 to now be a child of Entity3, I need a way to allow them to do this without needing to open up SQL Server or Visual Studio. This is not something that needs to be modified very often; maybe a few times a year, so the solution doesn't need to be super-fancy, just elegant. The only solution that I can think of is maybe allow the user to modify the structure from an XML file and and recreate or modify the SQL tables using SQLXMLBulkload. Any suggestions would be greatly appreciated. Thanks!
I would suggest you to create a separate mapping table that will have two columns parent id and child id. You can give your client an interface say with two drop downs (one to select parent and other to select client) and a save button (that will simply save this mapping.).
Hope this helps.
I've been asked by my boss to replicate an MS Access feature that we're going to lose shortly after migrating our product to .NET.
The feature is the ability to view and update any data in the database, particularly Tables or Views, in a tabular grid.
I can do it for pure tables that have a identity column because the SqlDataAdapter can auto-generate the relevant CRUD methods on the fly, to fill / update via DataTables.
However, views are somewhat more tricky. SQL Server Management Studio does allow it. If you click 'Edit top xx rows' on a View, it allows you to edit the data in some columns in what looks to be a standard .NET DataGridView - though it feels a bit magical.
So, a few questions:
How does SSMS infer which primary key to use, even if the key is not in the view?
How does SSMS determine which column inside a view can or can not be edited / inserted / deleted etc.?
What would be my best option to replicate this inside a .NET application?
Is it possible to connect a DataGridView to an old style oledb / obdc connection that has a constant direct connection to the database?
Any guidance as normal will be highly appreciated.
Marlon
SQL Server views can be updated just as if they were a single table, as long as they conform to certain conditions.
From the documentation:
Updatable Views
You can modify the data of an underlying base table through a view, as
long as the following conditions are true:
Any modifications, including UPDATE, INSERT, and DELETE statements,
must reference columns from only one base table.
The columns being modified in the view must directly reference the
underlying data in the table columns. The columns cannot be derived in
any other way, such as through the following:
An aggregate function: AVG, COUNT, SUM, MIN, MAX, GROUPING, STDEV,
STDEVP, VAR, and VARP.
A computation. The column cannot be computed from an expression that
uses other columns. Columns that are formed by using the set operators
UNION, UNION ALL, CROSSJOIN, EXCEPT, and INTERSECT amount to a
computation and are also not updatable.
The columns being modified are not affected by GROUP BY, HAVING, or
DISTINCT clauses.
TOP is not used anywhere in the select_statement of the view together
with the WITH CHECK OPTION clause.
The previous restrictions apply to any subqueries in the FROM clause
of the view, just as they apply to the view itself. Generally, the
Database Engine must be able to unambiguously trace modifications from
the view definition to one base table. For more information, see
Modify Data Through a View.
I don't believe SSMS is doing anything special - editing the contents of a view offers exactly the same functionality as editing the contents of a table. If the user attempts to make a change that does not conform to the above conditions, SSMS will likely display an error.
How does SSMS infer which primary key to use, even if the key is not in the view?
It doesn't. SQL Server does since only one underlying table can be edited at a time.
How does SSMS determine which column inside a view can or can not be edited / inserted / deleted etc.?
Again, it's SQL Server that determines this, not SSMS.
What would be my best option to replicate this inside a .NET application?
As long as all your views conform to the above conditions, simply do the same as you're doing for tables, but be ready to handle the errors from users doing something they can't (this implies some user training will be required, just as it would be if they were using SSMS directly).
My scenario.... I dynamically(dicval.add(Tkey,Tvalue) create set of columns and now i need to insert values and save those values using Stored procedures..how to achieve this
not sure exactly what you are trying to accomplish, what your schemas are, etc.
w/i dynamic sql, one can run DDL statements - create table, alter table add col, etc
from what little I see in your answer, you may be
If you are using linq, perhaps it contains structures for building tables?
Again, from what little I see of your question, maybe the correct question to be asking is, "how can I change the structure of this app so that I'm not having to dynamically add columns"
As far as interacting with dynamically, recently created tables via stored procedures - that seems to say to me that the sprocs would need to be dynamically created as well.
If you can't change the structure, and insist on doing some sort of string manipulation to create DDL of tables, columns and sprocs, I highly recommend you be very, very organized and make use of tokenized templates.
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.