Passing Connection String to Entity Framework 6

前端 未结 5 461
爱一瞬间的悲伤
爱一瞬间的悲伤 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.

    0 讨论(0)
  • 2020-12-14 08:36

    I have used the connection string like this, entity connection string instead of normal connection string

      SqlConnectionStringBuilder sqlString = new SqlConnectionStringBuilder()
            {
        DataSource = "SOURAV-PC", // Server name
        InitialCatalog = "efDB",  //Database
                UserID = "sourav",         //Username
                Password = "mypassword",  //Password
            };
            //Build an Entity Framework connection string
    
            EntityConnectionStringBuilder entityString = new EntityConnectionStringBuilder()
            {
                Provider = "System.Data.SqlClient",
                Metadata =   "res://*/testModel.csdl|res://*/testModel.ssdl|res://*/testModel.msl",
                ProviderConnectionString = sqlString.ToString()
            };
            return entityString.ConnectionString;
        }
    
    0 讨论(0)
  • 2020-12-14 08:42

    By convention, Entity Framework takes the connection string that has the same name as the context. For example:

    public cerviondemoEntities()
        : base("name=cerviondemoEntities")
    {
    }
    

    The DbContext class has a constructor that takes a connection string. You can add another constructor that takes a connectionstring as a parameter and pass it to the base constructor.

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

    Be sure to create a partial class so your added constructor is not overwritten.

    Sample ConnectionString:

    <connectionStrings>
        <add name="cerviondemoEntities" connectionString="data source=server\database;initial catalog=catalog;persist security info=True;user id=user;password=password;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" /> 
    </connectionStrings>
    
    0 讨论(0)
  • 2020-12-14 08:44

    I had this issue as well and used the method from Daniel in the comments.

    Alternatively, you can add it to the .tt file instead of creating another file and using 'partial' – Daniel K Dec 9 '16 at 19:16

    Update *.Context.tt File

    just replace the lines...

        public <#=code.Escape(container)#>()
        : base("name=<#=container.Name#>")
    {
    

    with the following...

    public <#=code.Escape(container)#>()
        : this("name=<#=container.Name#>")
    {
    }
    
    public <#=code.Escape(container)#>(String nameOrConnectionString)
        : base(nameOrConnectionString)
    {
    

    I hope this helps.

    0 讨论(0)
  • 2020-12-14 08:48

    You need to introduce another constructor in your context that is expecting a string connectionString argument and make it call base(string nameOrConnectionString):

    public cerviondemoEntities(string connectionString) : base(connectionString)
    {
    }
    
    0 讨论(0)
提交回复
热议问题