How do I refresh the relationships in a dataset? - visual-studio

I a working in VisualStudio 2005. I have a dataset with sevaral datatables in it already. I had to modify the database to add a new foreign key that I forgot about. How do I get visual studio to recognize the new relationship?

.Net does not load FK relationships into your DataSet automatically - however, you can add them yourself with a DataRelation*.
*this may not be true if you are using LINQ - if you are, I am unsure.

Right-click on the DataSet page, and select Add->Relation?
If you've defined it in your database, you can always re-drag the affected table back into the Dataset then re-enter your Queries.

Related

Visual Studio 2013 Dataset Designer refresh relations

I have an application with a dataset linked to an sql server database. I have updated some of the names or foreign keys and primary keys in the sql server. How do I make those changes translate to the data set. For example, I had a primary key called fk_temsempl_xxxxx. I changed it to fk_temsempl on the sql database. How do I get that change to show in the dataset designer in visual studio?
I have tried running custom tool by right clicking on the dataset and clicking run custom tool. That didnt work. I tried configuring the table adapter of one of the tables where a change occured, but the name of the relation didnt change.
You actually just right click the relation and choose Edit Relation... or double click on the line (when the mouse cursor changes from arrow to drag symbol) but I honestly wouldn't bother; you'll then have further refactoring to do in the code anywhere the relation is used, and it can be heavily used by visual designers.
You also get the problem that VS may not help you with the refactoring: in data binding scenarios most things that can be a source of data can also be a collection of multiple things that can be a valid DataSource. They then rely on a string DataMember to determine which of the collections of data in the data source should be used for the data.
For example, when a bindingsource is bound to list a DataTable, the bindingsource.DataSource property might be the DataSet object that contains the DataTable, and thebindingsource.DataMemberis a string of "YOUR_TABLE_NAME". the BindingSource might not be bound asmyBindignSource.DataSource = myDataSet.MyDataTable`. Refactoring inside strings involves a find and replace
DataRelations in a DataSet are created from foreign keys as they were discovered when the relevant table(s) were added to the dataset but it is important to note that, like DataTables and everything else, they are NOTHING to do with the database schema objects at all - they aren't permanently associated with them, the dataset entities are just set up looking something like the database objects when they (dataset entities) are first created. DataTables are created from only those columns selected, and whatever .NET datatypes closely resemble the types output by the query. For a table of:
Person
------
Name VARCHAR(50)
SSN INTEGER
Birthdate DATE
If you created the table with SELECT * FROM Person you'd get a datatable with Name (string), SSN (int), Birthdate (datetime) but if you made a new datatable in the dataset based on SELECT LEFT(Name, 1) as Initial, PADLEFT(SSN, 20) as PadSSN, DATEDIFF(day, Birthdate, NOW()) as AgeDays FROM Person then you'd get a datatable of Initial (string), PadSSN (string), AgeDays (int) - i.e. the datatable looks nothing like the db table. This concept of disconnection between dataset and db is pervasive, and really the only things that relate in any way to the database are the properties that specify which DB table/column a particular DataTable/DataColumn relates to for purposes of loading/saving data. Your Person.Name datacolumn can be renamed to Blahblah, but it will still have a .SourceColumn property that is set to "Name" - that's how the mapping between dataset and db works; dataset is predominantly completely independent of the db. Renaming a DB column would require a change to the SourceColumn property only
DataRelations don't even have this notion of linking to the parent relation in the database; there's no SourceRelation or SourceFK proeprty because there is no need to. They're set up with the same rules and a generated name all based on the rules of the FK, but then they function independently and only within the dataset. If you rename or even remove an FK from the db the dataset will carry on working in the same restricted way it always did; adding a datarow to a child table when no aprent row exists for it will throw an exception - none of it anything to do with the FK in the db, and the DataRelation can have different rules to the FK (e.g it can cascade deletes when the FK is NOACTION) or even different columns. You can have more or fewer DataRelations than the DB has FKs
Run Custom Tool is not a "contact the DB and see what changes have occurred there and replicate them into the dataset", it is a "turn the XSD that describes the dataset into a bunch of C# classes that implement strongly typed dataset/table/relation/column etc objects". Any time you change the XSD by making an edit in the visual designer and hit save, the custom tool is run. If you edit the XSD directly in a text editor you may need to run it manually to have your changes reflected in c# classes
Reconfiguring a tableadapter probably won't do anything to the relations either; its solely concerned with changing the datatable and tableadapter. If you really want to refresh the relations, delete the datatable from the set and recreate it. Be prepared for a potentially significant mop up/refactoring of code

How to join more than two tables using Lightswitch LINQ query

I am new to lightswitch as well as linq, i am using LS 2015. I want to know how to display the data of one table in other table's screen.
For example I have job vacancies, where I want to show candidate professional information who has applied for job. These two tables are related through Candidate personal ID, means a foreign key between candidate personal table and professional table.
The relation is as follows
JobVacancies->CandidatePersonalInfor->candidateProfessionalInfo. They are many to one related.
I have written a linq query to fetch data, where i have included some other tables also, my code is as follows
Any help is appreciated.
Have you defined the relationships you describe between the entities? If so, you'll have the ability to add and drop the details you require onto the form in the designer. The entities and their properties will be in the left pane of the screen designer.
If the join operation is more complex, you can use a WCF RIA Service to create a composite entity, as described in http://lightswitchhelpwebsite.com/Blog/tabid/61/EntryId/2226/Creating-a-WCF-RIA-Service-for-Visual-Studio-2013.aspx

How do I preserve my custom Linq to SQL class changes?

My MVC2 app use LINQ to SQL as the ORM. I just drag and drop the tables from the SQL Server Explorer connection to the LINQ design surface. Two of the tables (A and B) are related. Table A has 3 foreign keys referencing Table B. In the LINQ design surface, I manually change the name of the parent property for these associations to give them more distinguished names. However, whenever I modify the table structure in the SQL Server, and drag and drop the new structure to LINQ, I would lose the names. Is there a way to prevent this from happening? Thanks.
No. When you drag the tables back to the designer, Linq to SQL regenerates the code in the partial class, obliterating your custom changes.
The only way to preserve the name change is to rename the affected table fields in the database.
You might be able to achieve such customization preservation with the T4 Toolbox.
It is always a bad idea to make the associations at the LINQ level. Make the associations at the table level. This will ensure that when ever you are doing a drag and drop of the tables, the designer class reflects the correct associations.

Visual Studio 2010 DataCompare table comparison

In Visual Studio 2010 do you have the ability to compare the data of a database between 2 databases. I wanted to use this to copy data from one database to another. These databases have the exact same structure. But when I do a compare I see only 65 tables showing up in de DataCompare view in VS2010 but I have 66 tables in the database.
The next step was to find out which table not was showing up. The name of this table is 'CMS_PluginInstanceRouteParams'. This table is also present in both databases which I also have checked. The structure of this table is:
The FK is a relation to the PK of the CMS_PluginInstance table. The CMS_PluginInstance does show up in the Data Comparison window and the data is also compared and copied.
I also couldn't find a dialog or option where I can enter some table names to ignore.
Does anyone of you know why this table is not showing up in the list of tables in the Data Comparison window and why the data of this table not is compared.
I found the solution on http://social.msdn.microsoft.com/Forums/en-US/vstsdb/thread/d84a758e-79ea-4170-8807-7f8bdec98de1/:
Tables and views must meet two criteria to appear in the listing:
First, the schemas of the objects must match between the source and target database.
Second, only tables and views that have a primary key or a unique key appear in the list.
The DataCompare tool of VS2010 only compares tables that have unique keys. It is most likely that this missing table does not have a unique key. As for transferring data the best bet would be to use SQL Server Management Studios Import and Export tools. You should have no trouble finding guides online.

How to prevent Entity Designer from scripting the columns alphabetically?

I created 3 tables in SQL Designer -
Community { ID, Name, Description }
Audience { ID, Type, Value}
Community_Audience { CommunityID, AudienceID }
I imported this schema into the EDMX designer in Visual Studio 2010, right-clicked in the designer and selected "Generate Database from Model" to script out the schema. When I looked at the generated script, it is scripting it out like this -
Community { ID, Name, Description } -- Column names not sorted alphabetically.
Audience { ID, Type, Value} -- Column names not sorted alphabetically.
Community_Audience { AudienceID, CommunityID } -- Column names sorted alphabetically. CommunityID is after AudienceID.
This is causing my data seeding scripts to fail as I'm not specifying the column names -
INSERT INTO Community_Audience { 'guid', 'guid' }
I understand I can update my data seeding scripts to include column names or re-arrange the underlying xml in the edmx file but I don't want to go that route as I'll be clearing out the entities (tables) in the edmx and importing fresh schema from the database too often. I was wondering if there is setting that will prevent the designer from sorting alphabetically. Strangely, it only happens to the cross ref tables (like Community_Audience) and not the primary ones (like Community and Audience in this case).
Thanks for any help.
You have described both your choices - either modify your INSERT statements which should not be so hard and you will have to do it only once or try to modify EDMX (bad way if you use refreshing model from database). Entity designer really doesn't have a feature to define ordering in columns. Generating the database script is driven by separate component. It is based on Windows Workflow Foundation 4 and it can be replaced with your custom workflow but it is a lot of work without any real reason. The solution is modifying INSERT statements.
What I don't understand is why do you use Generate Database from Model in the same time as well as refreshing model from database.

Resources