Set Entity Framework Connection String at Runtime in C#

前端 未结 4 2075
孤街浪徒
孤街浪徒 2020-12-29 05:59

I need to set my Entity Framework connection string at runtime. Right now, I have the following:

string connectionString = \"metadata=res://*/DataModels.Cust         


        
4条回答
  •  温柔的废话
    2020-12-29 06:35

    set multiple connection strings in your web.config, and follow below:

    public partial class MyDatabaseEntities
    {
        public MyDatabaseEntities(string connection)
            : base(connection)
        {
        }
    }
    

    and then wherever you want to create instance of entities, pass connection string name in parameter:

    MyDatabaseEntities entities = new MyDatabaseEntities("CONNECTION_STRING_NAME");
    

    I hope this will help.

    Thanks

提交回复
热议问题