Programmatically creating a connection string for mapping an Entity Framework Code-First model with an existing Sql Server Compact database -
i've mapped entity framework code-first data model existing sql server compact database declarative approach using app.config
great if build such connection programmatically perhaps of entityconnectionstringbuilder
(system.data.entityclient) class. unfortunately approach not working [dbcontext].connection.connectionstring
not accepting of properties.
here's actual working code faulty 1 commented out:
book.cs
public class book { [key] public string isbn { get; set; } public string title { get; set; } public string author { get; set; } public string publisher { get; set; } public datetime published { get; set; } public int pages { get; set; } public bool instock { get; set; } public string description { get; set; } }
catalog.cs
public class catalog : dbcontext { public dbset<book> books { get; set; } }
app.config
<?xml version="1.0"?> <configuration> <connectionstrings> <add name="catalog" providername="system.data.sqlserverce.4.0" connectionstring="data source=res/catalog.sdf" /> </connectionstrings> </configuration>
main()
static void main() { // var res = path.combine(path.getdirectoryname(assembly.getexecutingassembly().location), "res"); // var str = new entityconnectionstringbuilder(); // str.name = "catalog"; // str.provider = "system.data.sqlserverce.4.0"; // str.providerconnectionstring = string.format("data source {0}", path.combine(res, "catalog.sdf")); try { using (var catalog = new catalog()) { // catalog.database.connection.connectionstring = str.connectionstring; // remaining code not relevant - skipped
i've tried using other builder classes such sqlceconnectionstringbuilder
(system.data.sqlserverce), sqlconnectionstringbuilder
(system.data.sqlclient) , dbconnectionstringbuilder
(system.data.common) apparently none of them seem match [dbcontext].connection.connectionstring
expecting.
should conclude there no programmatic way achieve this? advice surely appreciated. in advance contributions.
if using sql server compact 4.0 , dbcontext
code first cannot use entityconnectionstringbuilder
- builds connection string ef edmx file. need sqlceconnectionstringbuilder
system.data.sqlserverce
assembly version 4.0.0.0!
you should pass connection string context instance through constructor - dbcontext
has constructor accepting name of connection string (from configuration) or connection string itself.
Comments
Post a Comment