2

I have created my model using database first approach. The issue is that my connection string changes at runtime. Therefore I have added an overloaded constructor for the DBContext class that takes connection string.

public partial class MyDataContext: DbContext
    {
        public MyDataContext: ()
            : base("name=DbEntities")
        {
        }

        // added this overloaded contructor
        public MyDataContext: (string connectionString)
            : base(connectionString)
        {
        }

The connection string that I get at runtime is like below.

Integrated Security=SSPI;Data Source=localhost\sqlexpress;initial catalog=MyDatabase;Max Pool Size=100;Min Pool Size=20;Connect Timeout=15;

Howover when I run a query using MyDataContext that is created using overloaded constructor, I get following error.

System.Data.Entity.Infrastructure.UnintentionalCodeFirstException: The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development. This will not work correctly. To fix this problem do not remove the line of code that throws this exception. If you wish to use Database First or Model First, then make sure that the Entity Framework connection string is included in the app.config or web.config of the start-up project. If you are creating your own DbConnection, then make sure that it is an EntityConnection and not some other type of DbConnection, and that you pass it to one of the base DbContext constructors that take a DbConnection. To learn more about Code First, Database First, and Model First see the Entity Framework documentation here: http://go.microsoft.com/fwlink/?LinkId=394715

Any idea how this could be accomplished?

whoami
  • 1,689
  • 3
  • 22
  • 45
  • I suggest to start working from here http://msdn.microsoft.com/en-us/library/system.data.entityclient.entityconnectionstringbuilder(v=vs.110).aspx – Steve Dec 19 '14 at 19:02
  • i believe you need to include the metadata around the connection string when you're using an EDMX. Just look at the `DbEntities` connection string that was generated and go off that – Jonesopolis Dec 19 '14 at 19:03

1 Answers1

9

You need to create an EntityConnectionString and pass that one to your DbContext. You are currently only using an SqlConnectionString. I suggest you take a look at EntityConnectionStringBuilder.

This is an example which you could use for an Sql Database First approach;

SqlConnectionStringBuilder sqlStringBuilder = new SqlConnectionStringBuilder();
sqlStringBuilder.Database = "database name":
sqlStringBuilder.Password = "password";
sqlStringBuilder.UserID = "userid";
// other properties

EntityConnectionStringBuilder entityStringBuilder = new EntityConnectionStringBuilder();
entityStringBuilder.ProviderConnectionString = sqlBuilder.ConnectionString;
entityStringBuilder.Provider = "System.Data.MySqlClient";
entityStringBuilder.Metadata = "resx//*/YourDbContext.csdl|resx//*/YourDbContext.ssdl|resx//*/YourDbContext.msl";

MyDbContext dbContext = new MyDbContext(entityStringBuilder.ConnectionString);
Dion V.
  • 2,090
  • 2
  • 14
  • 24
  • Just to be clear its a SQL Sever database. My EF connection string is now coming as metadata=resx://*/;provider=System.Data.SqlClient;provider connection string="Integrated Security=SSPI;Data Source=localhost\sqlexpress;initial catalog=MyDatabase;Max Pool Size=100;Min Pool Size=20;Connect Timeout=15;" But I get following error when run a query. "At least one of the input paths is not valid because either it is too long or it has incorrect format." Any ideas what's wrong? – whoami Dec 19 '14 at 20:09
  • 1
    You should replace the metadata with correct metadata; usually it is something like `resx//*/YourDbContext.csdl|resx//*/YourDbContext.ssdl|resx//*/YourDbContext.msl`. You can probably find the correct metadata in your App.config. I'll update it in the answer. – Dion V. Dec 19 '14 at 20:15