以编程方式创建连接字符串,用于将实体框架代码优先模型与现有 Sql Server Compact 数据库映射
我已使用 app.config
通过声明性方法成功地将实体框架代码优先数据模型与现有 Sql Server Compact 数据库映射,但如果我能够以编程方式构建此类连接,那就太好了 也许需要 EntityConnectionStringBuilder (System.Data.EntityClient) 类的帮助。不幸的是,这种方法不起作用,因为 [DbContext].Connection.ConnectionString
不接受其大部分属性。
下面是实际工作代码,其中有问题的代码已被注释掉:
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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您首先使用 SQL Server Compact 4.0 和
DbContext
以及代码,则不能使用EntityConnectionStringBuilder
- 它使用 EDMX 文件为 EF 构建连接字符串。您需要来自System.Data.SqlServerCe
程序集的SqlCeConnectionStringBuilder
版本 4.0.0.0!您还应该通过构造函数将连接字符串传递到上下文实例 - DbContext 的构造函数接受连接字符串的名称(来自配置)或连接字符串本身。
If you are using SQL Server Compact 4.0 and
DbContext
with code first you cannot useEntityConnectionStringBuilder
- it builds connection string for EF with EDMX file. You needSqlCeConnectionStringBuilder
fromSystem.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.打开 DbConnection 并将其传递给 DbContext 构造函数或使用 DefaultConnectionFactory:
SqlCeConnectionFactory 是 Microsoft 提供的现成的连接工厂,但您也可以实现自己的工厂。
Open a DbConnection and pass it to the DbContext constructor or use a DefaultConnectionFactory:
SqlCeConnectionFactory is a ready-to-use connection factory provided by Microsoft, but you can also implement your own factory.