如何根据数据库结构的变化更新DataContext?

时间:2012-02-28 09:29:36

标签: c# visual-studio-2010 linq-to-sql

我正在使用linq-to-sql的DataContext在Visual Studio 2010中工作,它有几个映射到数据库中的表。现在,当我将某些内容更改为数据库的结构时,我注意到DataContext不会更改并导致错误。 DataContext不再与数据库的结构相对应。我通常通过删除DataContext中的所有表映射来解决此问题,然后再从Visual Studio中的数据库资源管理器中拖放它们。我觉得这非常麻烦,必须有更好的方法来做到这一点?当我更改数据库结构时,是否有自动更新DataContext的按钮或选项?

3 个答案:

答案 0 :(得分:3)

Linq2Sql模型在生成后与数据源断开连接。它只是在拖延和从数据源资源管理器中删除项目,建立连接并查询数据库模式。如果您的架构更改很小(即新的表列),则可以轻松地手动添加这些。对于更激烈的架构更改,您当前的方法可能是最快且最简单的。

可以使用sqlmetal.exe命令行工具自动执行此代码生成过程。我过去曾经使用过不断变化的数据库模式来处理项目,我们在每次构建之前都调用了sqlmetal,因此我们在更改时会遇到有用的编译错误。如果您的模式没有那么大的变化,您可以在项目中使用批处理文件来在必要时更新Linq2Sql模型。

答案 1 :(得分:0)

EF Core中,您可能会发现某个“脚手架”命令很有用。

脚手架可以重新生成DbContext以及模型。根据我的经验,不会覆盖您为扩展partial classes所做的任何自定义DbContext,因此这些会继续有效。

您可能需要通过将某些工具添加到project.json(旧)/ csproj(新)来安装某些工具

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. (E.g.  Microsoft.EntityFrameworkCore.SqlServer)

此命令(从项目的根目录运行,假设您将模型保存在名为“Models”的文件夹中); 1)更新我的模型和2)我的DbContext。如果您只想更新DbContext,我使用source-control(git)来放弃对模型的更改;保持对DbContext的更改。

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

Powershell的

More info here,缩写命令:

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. (E.g. 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.

答案 2 :(得分:-1)

让连接符为:     string pp = @&#34; 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&#34 ;;

并更新任务如下:

  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)
                {

                }

        }

而不是DataClasses1DataContext dc = new DataClasses1DataContext();

DataClasses1DataContext dc = new DataClasses1DataContext(pp);

Vby调用SubmitChanges()作为我们类对象的更新数据实际上是在实际数据库中编写的

相关问题