I'm new to entity developer and I'm trying to create databases and tables etc from code.
I managed to do this with sqlite like this after I created my edml model:
my appconfig file:
Code: Select all
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="EntitiesConnectionStringSQLite" connectionString="metadata=res://*/DataModel1.csdl|res://*/DataModel1.ssdl|res://*/DataModel1.msl;provider=Devart.Data.SQLite;provider connection string="Data Source=""C:\Users\oliver.perner\Documents\Visual Studio 2013\Projects\WindowsFormsAppEntDevArt\db\TestDbSQLite.sqlite"";FailIfMissing=False"" providerName="System.Data.EntityClient" />
<add name="EntitiesConnectionStringPostgre" connectionString="metadata=res://*/DataModel1.csdl|res://*/DataModel1.ssdl|res://*/DataModel1.msl;provider=Devart.Data.PostgreSql;provider connection string='user id=postgres;password=postgres;host=DEVELOP03;database=TestDbPostgreSQL;initial schema=public'" providerName="System.Data.EntityClient" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="Devart.Data.SQLite" type="Devart.Data.SQLite.Entity.SQLiteEntityProviderServices, Devart.Data.SQLite.Entity, Version=5.2.352.6, Culture=neutral, PublicKeyToken=09af7300eec23701" />
<provider invariantName="Devart.Data.PostgreSql" type="Devart.Data.PostgreSql.Entity.PgSqlEntityProviderServices, Devart.Data.PostgreSql.Entity, Version=7.3.352.0, Culture=neutral, PublicKeyToken=09af7300eec23701" />
</providers>
</entityFramework>
</configuration>
Code: Select all
private void btnCreateSQLite_Click(object sender, EventArgs e) {
var connectionString = ConfigurationManager.ConnectionStrings["EntitiesConnectionStringSQLite"].ConnectionString;
Entities lData = new Entities(connectionString);
lData.CreateDatabase();
}
Now I'm trying to do the same with PostgreSQL - which is not working
The difference is in Postgre I don't want to create the database but only the schema
I get an exception in this peace of code
Code: Select all
public Entities(string connectionString) :
base(connectionString, "Entities")
{"The 'Instance' member of the Entity Framework provider type 'Devart.Data.PostgreSql.Entity.PgSqlEntityProviderServices, Devart.Data.PostgreSql.Entity, Version=7.3.352.0, Culture=neutral, PublicKeyToken=09af7300eec23701' did not return an object that inherits from 'System.Data.Entity.Core.Common.DbProviderServices'. Entity Framework providers must inherit from this class and the 'Instance' member must return the singleton instance of the provider. This may be because the provider does not support Entity Framework 6 or later; see http://go.microsoft.com/fwlink/?LinkId=260882 for more information."}
my function
Code: Select all
private void btnCreatePostgre_Click(object sender, EventArgs e) {
var connectionString = ConfigurationManager.ConnectionStrings["EntitiesConnectionStringPostgre"].ConnectionString;Entities lData = new Entities(connectionString);
lData.CreateDatabaseScript();
}
Edit: The Creation of the SqLite db only works with the original connectionstring name that was created when I set up the model ("EntitiesConnectionString") - all my manually added connection strings do not work.
Just to get the picture clear: my target is to create a winforms gui with 3 buttons - each button should automatically create a database for sqlite or a schema for postgres and mssql server.
Any help or approach appreciated
thx in advance, Oliver