开发者

SQLMetal generates classes but not parameterless constructors

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 开发者_StackOverflow社区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.


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>


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜