locked
How to register a new System.Data.Linq.DataContext without empty constructor? RRS feed

  • Question

  • User-1735691636 posted

    I want to register a new context with a connectionstring from the configucation file.

    How to do this? 

    This doesn't work:

    MyOwnDataContect : System.Data.Linq.DataContext

    MyOwnDatacontext dataContext = new MyOwnDataContext(System.Configuration.ConfigurationManager.ConnectionString)
    model.RegisterContext(dataContext, new ContextConfiguration() { ScaffoldAllTables = true });
    
     

     

     

     

    Tuesday, November 25, 2008 8:50 AM

All replies

  • User-330204900 posted

    Hi m@rco, have a look at this thread here http://forums.asp.net/p/1330279/2668877.aspx where Scott Hunter suggests

    model.RegisterContext(() => new NorthwindDataContext("") , new ContextConfiguration() { ScaffoldAllTables = false });

    Hope this helps [:D]

    Tuesday, November 25, 2008 9:31 AM
  • User-1735691636 posted

    thanx for you reply!

    this works, but how...

     

    Tuesday, November 25, 2008 9:52 AM
  • User-1005219520 posted

    You are usually better off to pass the connection string explicitly. You can generate your various connection strings in global.asax or store them in a configuration file. The snippet below generates a connection string using integrated security and one using SQL security (each string could easily resolve to different DBs).

     

    string conStrIntegratedSecurity = new System.Data.EntityClient.EntityConnectionStringBuilder
                {
                    Metadata = "res://*/LT08.csdl|res://*/LT08.ssdl|res://*/LT08.msl",
                    Provider = "System.Data.SqlClient",
                    ProviderConnectionString = new System.Data.SqlClient.SqlConnectionStringBuilder
                    {
                        InitialCatalog = "AdventureWorksLT2008",
                        DataSource = "bing0",
                        IntegratedSecurity = true,
                    }.ConnectionString
                }.ConnectionString;
    
    
                string connectionString = new System.Data.EntityClient.EntityConnectionStringBuilder
                {
                    Metadata = "res://*/LT08.csdl|res://*/LT08.ssdl|res://*/LT08.msl",
                    Provider = "System.Data.SqlClient",
                    ProviderConnectionString = new System.Data.SqlClient.SqlConnectionStringBuilder
                    {
                        InitialCatalog = "AdventureWorksLT2008",
                        DataSource = "bing0",
                        IntegratedSecurity = false,
                        UserID = getUID(),                 // User ID such as "sa"
                        Password= getPWD(),               // hide the password
                    }.ConnectionString
                }.ConnectionString;

     Now just pass in the conections string you want

     
     string
    conStr = connectionString ; //conStr = "Data Source=ricka0;Initial Catalog=AdventureWorksLT;" // + "Persist Security Info=True;User ID=sa;Password=*(IU89iu;"; model.RegisterContext(() => new DataClassesDataContext(conStr), new ContextConfiguration() { ScaffoldAllTables = true });

     

    Tuesday, November 25, 2008 1:36 PM