Strongly typed datasets and schema changes

2020-07-13 08:08发布

问题:

I have a project which uses strongly typed datasets.

Let's assume I want to change the database schema of the database used by this application. I have a table named Country and I want to add a new column named "IsADemocracy" (that's an example) of the SQL Server datatype BIT.

Now, how do I update the strongly typed dataset so that I can use this new field in the application?

I've tried adding the column in the Country table on the dataset, gave it the type of boolean and mapped its "Source" property to "IsADemocracy".

I can access the field in the application, give it a value, etc, but the underlying queries made by the table adapter have not been updated to reflect this change. They are as they were before I added this column in the strongly typed dataset. I hoped what I did would be enough but it does not seem to be the case.

Should I regenerate the strongly datasets classes somehow? If so, how and what are the implications?

I could probably delete the Country table in the Dataset designer, and redrop it from the Server Explorer, but I have customer queries on it and I don't want to lose them.

Thanks.

回答1:

  1. First, make sure you have a backup

  2. Right click on the .xsd node in Solution Explorer and select "Run Custom tool"

But be careful with your Custom Queries. They have a tendency to disappear without warning. Not always.

This is one good reason to fill in those queries form the partial class 'main' file.



回答2:

Another suggestion - right click on the Table's representation in the designer i.e the xsd, and use the Add Option, to add a Column. Once you have the column, you need to set its properties as by default a new column gets added as a string. Doing it this way means you retain any custom queries etc that have been added earlier.

This allows you to add columns, obviously deleting is more straightforward.