SQLMetal 生成类,但不生成无参数构造函数

发布于 2024-10-21 10:27:51 字数 1144 浏览 1 评论 0 原文

我需要定期刷新我的 Linq To SQL 类;是的,我感到羞耻,因为我没有足够彻底地考虑我的数据模式,糟糕的开发人员,令人作呕。我发现 SQLMetal 几乎可以解决问题,但也许我在参数列表中遗漏了一些东西。

当我使用 Visual Studio 外部工具从闪亮的新工具栏按钮运行批处理文件时,

@echo off
del c:\path\to\LinqToSql.dbml
SQLMetal.exe /server:SERVER\SQLSERVER /database:db /timeout:0 /dbml:"c:\path\to\LinqToSql.dbml" /namespace:DAL /context:DataDataContext /entitybase:System.Data.Linq.DataContext /language:csharp /pluralize

SqlMetal 会生成 .dbml 文件,万岁。但是,问题 1 我可以通过编程方式将 .dbml 文件包含到我的项目中吗?

问题 2

为什么当我手动包含新生成的 .dbml 文件后进行编译时,我的每个类都会出现以下与其无参数构造函数的行号相关的构建错误?例如 30 个表 = 30 个构建错误。

'System.Data.Linq.DataContext' does not contain a constructor that takes 0 arguments

实际上,

我确实注意到我的 DataDataContext 生成的类没有无参数构造函数,因此我添加了一个部分类来补充,但它仍然无法解决问题。

public partial class DataDataContext
{
    public DataDataContext() :
        base(global::DAL.Properties.Settings.Default.MyConnectionString, mappingSource)
    {
        OnCreated();
    }
}

我认为此刷新过程能够自动化,但手动添加生成的 .dbml 文件(会产生这些构造函数错误)对我来说不起作用。

I need to regularly refresh my Linq To SQL classes; Yes, shame on me for not thinking about my data schema thoroughly enough, bad developer, ad nauseum. I found SQLMetal almost does the trick, but maybe I'm missing something from the parameter list.

When I run my batch file from my shiny new toolbar button using Visual Studio External Tools,

@echo off
del c:\path\to\LinqToSql.dbml
SQLMetal.exe /server:SERVER\SQLSERVER /database:db /timeout:0 /dbml:"c:\path\to\LinqToSql.dbml" /namespace:DAL /context:DataDataContext /entitybase:System.Data.Linq.DataContext /language:csharp /pluralize

SqlMetal generates the .dbml file, hooray. However, Question 1 can I programmatically include the .dbml file into my project?

Question 2

Why, when I compile after manually including the newly generated .dbml file, do each of my classes have the following build errors associated with the line number of their parameterless constructors? e.g. 30 tables = 30 build errors.

'System.Data.Linq.DataContext' does not contain a constructor that takes 0 arguments

The actual

I did notice my DataDataContext generated class is without a parameterless constructor, so I added a partial class to supplement, but it still doesn't do the trick.

public partial class DataDataContext
{
    public DataDataContext() :
        base(global::DAL.Properties.Settings.Default.MyConnectionString, mappingSource)
    {
        OnCreated();
    }
}

I thought this refresh process would be able to be automated, but manually adding the generated .dbml file that produces these constructor errors isn't working for me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

帅气称霸 2024-10-28 10:27:51

简短的回答是 SQLMetal 不执行无参数构造函数。答案的其余部分是您的分部类应该可以工作,但它应该如下所示:

namespace DAL {
    public partial class DataDataContext
    {
        public DataDataContext() :
            this(/* Get your connection string here */)
        {
            OnCreated();
        }
    }
 }

兴趣点:

  • 调用 this(string) 构造函数而不是基类,以便
    如果您选择,可以正确连接 OnCreated 事件。
  • 获取您想要的连接字符串 - 一个简单的方法是

    ConfigurationManager.ConnectionStrings["MyConnectionSTRing"].ConnectionString

与 app.config 中的以下内容相结合:

<connectionStrings>
    <add
        name="MyConnectionSTring"
        connectionString="Data Source=SQLServerName\instance;Initial Catalog=DatabaseName;Persist Security Info=True;User ID=user"
        providerName="System.Data.SqlClient" /> 
</connectionStrings>

The short answer is that SQLMetal doesn't do parameterless constructors. The rest of the answer is that your partial class should work, but it should look like this:

namespace DAL {
    public partial class DataDataContext
    {
        public DataDataContext() :
            this(/* Get your connection string here */)
        {
            OnCreated();
        }
    }
 }

Points of interest:

  • Call the this(string) constructor instead of base so that the
    OnCreated event can be wired up correctly if you choose to.
  • Get the connection string however you want - a simple way would be

    ConfigurationManager.ConnectionStrings["MyConnectionSTring"].ConnectionString

combined with the following in app.config:

<connectionStrings>
    <add
        name="MyConnectionSTring"
        connectionString="Data Source=SQLServerName\instance;Initial Catalog=DatabaseName;Persist Security Info=True;User ID=user"
        providerName="System.Data.SqlClient" /> 
</connectionStrings>
梦巷 2024-10-28 10:27:51

我使用分部类来创建无参数构造函数(就像您所做的那样)并且没有任何问题。

请注意,您正在 DAL 命名空间中创建 DataDataContext。 (上面的代码中缺少该内容)

namespace DAL {
    public partial class DataDataContext
    {
        public DataDataContext() :
            base(global::DAL.Properties.Settings.Default.MyConnectionString, mappingSource)
        {
            OnCreated();
        }
    }
 }

I use a partial class to create the parameterless constructor (as you do) and don't have any problems.

Beware that you are creating the DataDataContext in the DAL namespace. (that is missing in your code above)

namespace DAL {
    public partial class DataDataContext
    {
        public DataDataContext() :
            base(global::DAL.Properties.Settings.Default.MyConnectionString, mappingSource)
        {
            OnCreated();
        }
    }
 }
心碎无痕… 2024-10-28 10:27:51

1)您真的需要 .dbml 文件吗?也许您可以直接使用类似的东西生成必要的 ORM 文件(并将其包含到您的解决方案中):

SQLMetal.exe /server:SERVER\SQLSERVER /database:DB /namespace:DAL /code:YourDatamapClass.cs /language:csharp

然后使批处理文件将现在刷新的类移动到其正确的位置(可能到解决方案文件夹)。

2)你真的需要一个无参数构造函数吗?例如,将数据库连接字符串放入配置文件并用它创建映射器对象:

//GetConnectionString (not included here) gets the connection string from config
DB context = new DB(GetConnectionString()); 

1) Do you really need the .dbml file? Maybe you could generate the necessary ORM file directly with something like this (and have it included to your solution):

SQLMetal.exe /server:SERVER\SQLSERVER /database:DB /namespace:DAL /code:YourDatamapClass.cs /language:csharp

Then make the batch file move the now refreshed class to its correct place (to the solution folder probably).

2) Do you really need a parameterless constructor? For example, put the DB connection string to your config file and create the mapper object with it:

//GetConnectionString (not included here) gets the connection string from config
DB context = new DB(GetConnectionString()); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文