Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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.

like image 389
David Fox Avatar asked Mar 13 '11 19:03

David Fox


1 Answers

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>
like image 199
Peter T. LaComb Jr. Avatar answered Nov 02 '22 11:11

Peter T. LaComb Jr.