Click here to Skip to main content
15,868,164 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
hello
i wanna use sqlconnection in my CodeFirst Mvc Project.
my connectionstring is not in web.config.
i see something like below in ProductContecxt:

VB
public ProductContext()
:base(SharedDll.Connections.ConnectionString())

is it correct?

how can i use it in my connection string like before i used.
C#
//before:
 String connectionstring = "DataSource=.\SQLEXPRESS;"
                            + "Initial Catalog=DEMO;"
                            + "Integrated Security=True";


but now how can use public ProductContext()... in connectionstring
Posted
Comments
Kuthuparakkal 8-Aug-13 1:41am    
Post parent class(base) definition

1 solution

You can use like below (this is a sample).

Global.asax.cs

C#
protected void Application_Start()
 {
  DataCatalog.ConnectionString = ConfigurationManager.ConnectionStrings["PawLoyalty"].ConnectionString;
 }



Web.config

XML
<connectionStrings>
    <add name="PawLoyalty" connectionString="Server=.;database=PawLoyalty;Trusted_connection=true;pooling=true;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
    </connectionStrings>


DataCatalog.cs

C#
public class DataCatalog : DbContext
    {
        public static string ConnectionString { get; set; }

        public DataCatalog()
            : base(ConnectionString)
        {

        }

}



I hope this will help to you.
 
Share this answer
 
Comments
Member 8454063 8-Aug-13 5:54am    
hi my good friends.
i used using (SqlConnection connection = new SqlConnection(SharedDll.Connections.ConnectionString()))
{...
}
and it worked . thanks
Sampath Lokuge 8-Aug-13 6:05am    
But that is not the best practice when you use EF code first.B'cos when you use db operation each and every time you have to create SqlConnection object and it doesn't cache itself.So it'll affect your app over roll performances.And put that code on every places are not nice at all.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900