【问题标题】:How to update DataContext based on changes in the database structure?如何根据数据库结构的变化更新DataContext?
【发布时间】:2012-02-28 09:29:36
【问题描述】:

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

【问题讨论】:

  • @Niklas 很高兴听到另一个人有类似的问题,但后来Entity Framework。但公认的解决方案或多或少是我所做的。但是当多个表发生变化并且您不知道哪些表时,这有点麻烦。您最终将每次都删除并添加所有这些。我想一定有更好的方法?
  • @MattDavey 确实看起来像复制品。我只是没有设法制定我的标题,以便在提出我的问题时提出您的文章作为建议。随意投票关闭。

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


【解决方案1】:

Linq2Sql 模型一旦生成就会与数据源断开连接。只有在从数据源浏览器中拖放项目时,才会建立连接并查询数据库模式。如果您的架构更改很小(即新表列),手动添加这些很容易。对于更剧烈的架构更改,您当前的方法可能是最快和最简单的。

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

【讨论】:

  • 我会试试sqlmetal,我读了你的文章,相信这就是我所追求的。
【解决方案2】:

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

脚手架可以重新生成您的DbContext 以及您的模型。根据我的经验,它不会覆盖您为扩展 DbContext 而创建的任何自定义 partial classes,因此它们会继续工作。

您可能需要通过将某些工具添加到您的 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)

此命令(从项目的根目录运行,假设您将模型保存在名为“模型”的文件夹中); 1) 更新我的模型和 2) 我的 DbContext。如果您只想更新 DbContext,我使用源代码控制 (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.

【讨论】:

    【解决方案3】:

    让connectionstrin为: 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" ;

    更新任务如下:

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

    通过调用SubmitChanges()我们类的对象的更新数据实际上正在写入实际数据库中

    【讨论】:

    • 你好像根本没看懂这个问题。
    猜你喜欢
    • 2017-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-04
    • 1970-01-01
    相关资源
    最近更新 更多