Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 736
felix Antony Avatar asked Dec 19 '25 13:12

felix Antony


1 Answers

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

like image 52
felix Antony Avatar answered Dec 24 '25 11:12

felix Antony



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!