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

Last post 11-25-2008 1:36 PM by ricka6. 3 replies.

Sort Posts:

  • How to register a new System.Data.Linq.DataContext without empty constructor?

    11-25-2008, 8:50 AM
    • Member
      13 point Member
    • m@rco
    • Member since 11-25-2008, 1:29 PM
    • Posts 36

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

     

     

     

  • Re: How to register a new System.Data.Linq.DataContext without empty constructor?

    11-25-2008, 9:31 AM
    • Star
      12,346 point Star
    • sjnaughton
    • Member since 04-29-2008, 5:11 PM
    • Newton-le-Willows, Merseyside, UK
    • Posts 2,571
    • TrustedFriends-MVPs

    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 Big Smile

    Steve Big Smile

    Always seeking an elegant solution.
    [Oh! If olny I colud tpye!]
    c# Bits blog
    Oh, and don't forget to mark as answer any posts that help you Big Smile
  • Re: How to register a new System.Data.Linq.DataContext without empty constructor?

    11-25-2008, 9:52 AM
    • Member
      13 point Member
    • m@rco
    • Member since 11-25-2008, 1:29 PM
    • Posts 36

    thanx for you reply!

    this works, but how...

     

  • Re: How to register a new System.Data.Linq.DataContext without empty constructor?

    11-25-2008, 1:36 PM
    • Contributor
      5,874 point Contributor
    • ricka6
    • Member since 06-25-2008, 6:04 PM
    • Redmond
    • Posts 941
    • AspNetTeam
      Moderator

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

     

    Rick -ASP.Net UE MVC FAQ   Rick on MVC & Dynamic Data   
Page 1 of 1 (4 items)