How to update a DataContext based on changes in the database structure?

I work in Visual Studio 2010 using the linq-to-sql DataContext , which has several maps for tables in the database. Now, when I change something to the database structure, I noticed that the DataContext does not change and leads to errors. DataContext no longer matches database structure. I usually solve this problem by deleting all tables in the DataContext and dragging them and deleting them again from Database Explorer in Visual Studio. I just feel that it is very bulky and what should be the best way to do this? Is there a button or option to automatically update the DataContext when I change the structure of the database?

+4
source share
3 answers

Linq2Sql models are disconnected from the data source after they are created. It is only at the point of drag and drop of elements from the explorer of the data source in which the connection is made, and the database schema is requested. If your schema changes are small (i.e., the new table column), simply add them manually. For more radical circuit changes, your current method is probably the fastest and easiest.

You can automate this code generation process using the sqlmetal.exe command-line tool . I worked on projects in the past with database schemas that were constantly changing, and we called sqlmetal before each build, so we got useful compilation errors when changing it. If your schema has not changed so much, you can simply have a batch file in your project to update the Linq2Sql model if necessary.

+3
source

In the EF Core you can find a specific scaffolding command.

Forests can repair your DbContext as well as your models. And in my experience, it won’t cancel any custom partial classes that you made for the DbContext extension, so they continue to work.

You may need to install certain tools by adding them to your project.json (old) / csproj (new)

dotnet cli

 dotnet ef dbcontext scaffold --help` Usage: dotnet ef dbcontext scaffold [arguments] [options] Arguments: <CONNECTION> The connection string to the database. <PROVIDER> The provider to use. (Eg Microsoft.EntityFrameworkCore.SqlServer) 

This command (executed from the project root directory if you keep your models in the Models folder); 1) updates my models and 2) my DbContext. If you only need updates for your DbContext, I use source-control (git) to undo the changes to the models; save changes to DbContext.

 dotnet ef dbcontext scaffold "{connection}" Microsoft.EntityFrameworkCore.SqlServer \ -f --output-dir=Models 

Powershell

Additional information here , abbreviated command:

 SYNTAX Scaffold-DbContext [-Connection] <String> [-Provider] <String> [-OutputDir <String>] [-Context <String>] [-Schemas <String[]>] [-Tables <String[]>] [-DataAnnotations] [-Force] [-Environment <String>] [-Project <String>] [-StartupProject <String>] [<CommonParameters>] PARAMETERS -Connection <String> The connection string to the database. -Provider <String> The provider to use. (Eg Microsoft.EntityFrameworkCore.SqlServer) -OutputDir <String> The directory to put files in. Paths are relaive to the project directory. -Context <String> The name of the DbContext to generate. .... -Force [<SwitchParameter>] Overwrite existing files. 
0
source

let connectionstrin be: string pp = @ "Data source = (LocalDB) \ v11.0; AttachDbFilename = C: \ Program Files \ Microsoft SQL Server \ MSSQL11.MSSQLSERVER \ MSSQL \ DATA \ AdventureWorks2012_Data.mdf; Integrated Security = True; time connection waiting = 30 ";

and the update task will be lower:

  public async Task callupdate() { try { int ppp = Convert.ToInt32(textBox1ID.Text); DataClasses1DataContext dc = new DataClasses1DataContext(pp); Person person = dc.Persons.Single(c => c.BusinessEntityID == ppp); person.PersonType = Convert.ToString(PersonTypecomboBox1.SelectedItem); person.PersonType = Convert.ToString(PersonTypecomboBox1.SelectedItem); if (NameStylecomboBox1.SelectedText == "False") person.NameStyle = false; else person.NameStyle = true; person.Title = Convert.ToString(TitlecomboBox1.SelectedItem); person.FirstName = FirstNametextBox2.Text; person.MiddleName = MiddleNametextBox3.Text; person.LastName = LastNametextBox4.Text; person.Suffix = SuffixtextBox5.Text; person.EmailPromotion = Convert.ToInt32(EmailPromotiontextBox6.Text); person.ModifiedDate = DateTime.Today; dc.SubmitChanges(); } catch(Exception exp) { } } 

instead of DataClasses1DataContext dc = new DataClasses1DataContext();

DataClasses1DataContext dc = new DataClasses1DataContext(pp);

Vby calls SubmitChanges() update data that is an object of our class is actually written to the actual database

-one
source

Source: https://habr.com/ru/post/1398780/


All Articles