EF 5 + SQL Server CE 4: How to specify custom location for database file?

前端 未结 4 1498
醉梦人生
醉梦人生 2020-12-15 13:32

I am developing a client system that needs a small local database. I want to avoid installation of SQL Server Express and have decided to go with SQL Server 4.

I use

4条回答
  •  有刺的猬
    2020-12-15 13:58

    In EF 6 this can be done with DbConfiguration:

    App.config:

    
    
    

    And inside your assembly create a class like:

    public class MyDbConfiguration : DbConfiguration
    {
        public MyDbConfiguration()
        {
            SetProviderServices(SqlCeProviderServices.ProviderInvariantName, SqlCeProviderServices.Instance);
            var directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            var path = Path.Combine(directory, @"ApplicationName\MyDatabase.sdf");          
            var connectionString = string.Format(@"Data Source={0}",path);
            SetDefaultConnectionFactory(new SqlCeConnectionFactory(SqlCeProviderServices.ProviderInvariantName, "", connectionString));
        }
    }  
    

提交回复
热议问题