SQLMetal 生成类,但不生成无参数构造函数
我需要定期刷新我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
简短的回答是 SQLMetal 不执行无参数构造函数。答案的其余部分是您的分部类应该可以工作,但它应该如下所示:
兴趣点:
如果您选择,可以正确连接 OnCreated 事件。
获取您想要的连接字符串 - 一个简单的方法是
ConfigurationManager.ConnectionStrings["MyConnectionSTRing"].ConnectionString
与 app.config 中的以下内容相结合:
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:
Points of interest:
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:
我使用分部类来创建无参数构造函数(就像您所做的那样)并且没有任何问题。
请注意,您正在
DAL
命名空间中创建DataDataContext
。 (上面的代码中缺少该内容)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 theDAL
namespace. (that is missing in your code above)1)您真的需要 .dbml 文件吗?也许您可以直接使用类似的东西生成必要的 ORM 文件(并将其包含到您的解决方案中):
然后使批处理文件将现在刷新的类移动到其正确的位置(可能到解决方案文件夹)。
2)你真的需要一个无参数构造函数吗?例如,将数据库连接字符串放入配置文件并用它创建映射器对象:
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):
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: