How do I create connection string programmatically to MS SQL in Entity Framework 6?

后端 未结 5 1709
一整个雨季
一整个雨季 2021-02-14 02:55

How do I create connection string programmatically to MS SQL in Entity Framework 6?

I\'m using c# and WPF and I was wondering if someone could show me how or link me to

5条回答
  •  长情又很酷
    2021-02-14 03:07

    I previously used the DefaultConnection string found in the app.config ( or web.config) as example and just replaced the "connectionstring" on the DbContext, to the one i wanted.

    The connectionstring looked something like :

    Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-ShoppingList.Web-20150903103641.mdf;Initial Catalog=aspnet-ShoppingList.Web-20150903103641;Integrated Security=True
    

    If i'm remembering correctly, you should replace the connectionstring in :

    DbContext.Database.Connection.Connectionstring
    

    PS. You can only use it this way if you are using Code-First. If you are using Model-First or Database-First you should use the EntityConnectionStringBuilder .

提交回复
热议问题