SQL CE 3.5 & EntityFramework

本秂侑毒 提交于 2019-12-25 03:05:40

问题


I have an sqlce 3.5 local db file in my VS2010 solution used with an EF4 Model. I need to protect my db with a password but do not serialize it in app.config. I also need to set up the maximum database file size to it's maximum allowed value which is 4091 MB when I try to set the connectionString in code, I can't find a way to set the 'Max Database Size=4091'.

Here is a method that I use to build the connection string

private string GetConnectionString() {
        // Specify the provider name, server and database.
        string providerName = "System.Data.SqlServerCe.3.5";
        string dataSource = @"|DataDirectory|\Database\dbFileName.sdf";
        string password = "mypassword";
        // Initialize the connection string builder for the underlying provider.
        SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();
        // Set the properties for the data source.
        sqlBuilder.DataSource = dataSource;
        sqlBuilder.Password = password;
        // Build the SqlConnection connection string.
        string providerString = sqlBuilder.ToString();

        // Initialize the EntityConnectionStringBuilder.
        EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
        //Set the provider name.
        entityBuilder.Provider = providerName;
        // Set the provider-specific connection string.
        entityBuilder.ProviderConnectionString = providerString;
        // Set the Metadata location.
        entityBuilder.Metadata = @"res://*/Models.Model1.csdl|res://*/Models.Model1.ssdl|res://*/Models.Model1.msl";
        return entityBuilder.ToString();
    }

Does anyone have any idea how I should do this? Thank you very much

Any other way to get:

  • Password protection + password set up in code

  • Maximum db file = 4091 MB


回答1:


Do not use the SqlConnectionStringBuilder, it is designed for SQL Server connections, not for SQL Server Compact. Simply set

string providerString = @"Data Source=|DataDirectory|\Database\dbFileName.sdf;Password=abc;Max Database Size=4091"; 



回答2:


I am getting the same error when I use DbContext constructor and have tried using different connection strings. A similar connection string works fine if I use SqlCeConnection. This works fine

SQL_CONNECTION_STRING = String.Format("password={0};Persist Security Info=True;Data Source={1};Max Database Size=1024", scsb.Password, ccdaSetup.DataSource);


来源:https://stackoverflow.com/questions/3556509/sql-ce-3-5-entityframework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!