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

前端 未结 3 483
天涯浪人
天涯浪人 2021-01-19 00:25

I\'m working in Visual Studio 2010 using linq-to-sql\'s DataContext which has several maps to tables in the database. Now when I change something to the structu

3条回答
  •  鱼传尺愫
    2021-01-19 00:43

    let the 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;Connect Timeout=30";

    and update task be the below mentioned :

      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 calling SubmitChanges() the update data that is the object of our class is actually being written in the actual database

提交回复
热议问题