Passing Connection String to Entity Framework 6

前端 未结 5 475
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-14 08:07

I am using EF6 in a class library (database first)

When I followed the wizard and added my tables I selected not to store the connections string in the app.config an

5条回答
  •  渐次进展
    2020-12-14 08:25

    So much better way to do this. The issue with any change of the diagram or properties the context.cs is rewritten. The clue is that all of the files are written as partial class objects. So, in order not to need to rewrite the context every time you change the entity framework, add a new cs file outside the EntityFramework edmx with a partial class with just the snippets discussed in previous posts.

        public partial class cerviondemoEntities
        {
           public cerviondemoEntities(string connectionString):base(connectionString){}
        }
    

    This was kind of discussed in the most approved post, but it was easy to glance over. This should stop your code from not working if you change the EntityFramework properties. This was important to me because my application needed to connect to remote SQL databases.

提交回复
热议问题