以编程方式创建一个连接字符串,用于将Entity Framework Code-First模型与现有的Sql Server Compact数据库进行映射 [英] Programmatically creating a connection string for mapping an Entity Framework Code-First model with an existing Sql Server Compact database
问题描述
我已经使用 app.config
以声明方式成功映射了一个具有现有Sql Server Compact数据库的Entity Framework Code-First数据模型,但是如果我可以通过 EntityConnectionStringBuilder
(System.Data.EntityClient)类的帮助,以编程方式来构建这种连接。不幸的是,这种方法不起作用,因为 [DbContext] .Connection.ConnectionString
不接受其大部分属性。
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
我尝试使用其他构建器类,如 SqlCeConnectionStringBuilder
(System.Data.SqlServerCe), SqlConnectionStringBuilder
(System.Data.SqlClient)甚至 DbConnectionStringBuilder
(System.Data.Common),但显然他们似乎没有匹配什么 [DbContext] .Connection.ConnectionString
期待。
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.
推荐答案
如果您使用SQL Server Compact 4.0和 DbContext
代码首先你不能使用 EntityConnectionStringBuilder
- 它使用EDMX文件构建EF的连接字符串。您需要 SqlCeConnectionStringBuilder
从 System.Data.SqlServerCe
版本4.0.0.0的程序集!
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!
您还应该通过构造函数将连接字符串传递给上下文实例 - DbContext
具有构造函数接受的名称连接字符串(从配置)或连接字符串本身。
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.
这篇关于以编程方式创建一个连接字符串,用于将Entity Framework Code-First模型与现有的Sql Server Compact数据库进行映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!