Set Entity Framework Connection String at Runtime in C#

前端 未结 4 2049
孤街浪徒
孤街浪徒 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:36

    I know this was asked 10 months ago, but I found an easier way to specify the connectionString:

    If your config file has it as:

    
    
    

    You can specify it as -

    public const string ConnectionString = @"name=CustomerDataModel";
    ..
    CustomerDBContext context = new CustomerDBContext(ConnectionString );
    

    No need to worry about quotes. Lot cleaner.

提交回复
热议问题