felix Antony
felix Antony

Reputation: 1460

Entity framework connection string reference from another project

I have a solution consisting of 4 projects. MVC, WCF, Business LYR, DataAcess. I am using entity framework for database transaction. My requirement is that i want to fetch the entity connectionstring only from MVC webconfig without refering in APP.cofig of acess layer. Is it possible in this scenario?

While I tried the following code I got an error.

    this.ConnectionString="data source=cmh-sosql;initial catalog=Student;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework";

            System.Data.SqlClient.SqlConnectionStringBuilder scsb = new System.Data.SqlClient.SqlConnectionStringBuilder(this.ConnectionString);

EntityConnectionStringBuilder ecb = new EntityConnectionStringBuilder();
ecb.Metadata = "res://*/schoolModel.csdl|res://*/schoolModel.ssdl|res://*/schoolModel.msl";
ecb.Provider = "System.Data.SqlClient";
ecb.ProviderConnectionString = scsb.ConnectionString;

using (SchoolDB schoolDB = new SchoolDB(ecb.ConnectionString))

Error: The entity type student is not part of the model for the current context.

Upvotes: 3

Views: 1858

Answers (1)

felix Antony
felix Antony

Reputation: 1460

You are absolutely correct. I got the solution. There is no need to keep any string in webconfig for reference to a entity model. We can use the above code for reference it. But the change is to configure the context object.

public SchoolDB(string  connectionString)
        : base(connectionString)
    {
    }
We need to change the constructor also by this format. 

thanks Sampath

Upvotes: 2

Related Questions