Programmatically creating a connection string for mapping an Entity Framework Code-First model with an existing Sql Server Compact database

北城余情 提交于 2020-01-05 07:38:36

问题


I've successfully mapped an Entity Framework Code-First data model with an existing Sql Server Compact database by a declarative approach using app.config but it would be great if I could build such connection programmatically with perhaps the help of the EntityConnectionStringBuilder (System.Data.EntityClient) class. Unfortunately this approach is not working as the [DbContext].Connection.ConnectionString is not accepting most of its properties.

Here's the actual working code with the faulty one 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 as SqlCeConnectionStringBuilder (System.Data.SqlServerCe), SqlConnectionStringBuilder (System.Data.SqlClient) and even DbConnectionStringBuilder (System.Data.Common) but apparently none of them seem to match what [DbContext].Connection.ConnectionString is expecting.

Should I conclude there is no programmatic way to achieve this? Any advice will be surely appreciated. Thanks much in advance for your contributions.


回答1:


If you are using SQL Server Compact 4.0 and DbContext with code first you cannot use EntityConnectionStringBuilder - it builds connection string for EF with EDMX file. You need SqlCeConnectionStringBuilder from System.Data.SqlServerCe assembly with version 4.0.0.0!

You should also pass the connection string to the context instance through the constructor - DbContext has constructor accepting name of the connection string (from configuration) or connection string itself.




回答2:


Open a DbConnection and pass it to the DbContext constructor or use a DefaultConnectionFactory:

Database.DefaultConnectionFactory
    = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0");

using (var catalog = new Catalog("Catalog.sdf"))
{
}

SqlCeConnectionFactory is a ready-to-use connection factory provided by Microsoft, but you can also implement your own factory.



来源:https://stackoverflow.com/questions/6558318/programmatically-creating-a-connection-string-for-mapping-an-entity-framework-co

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