以编程方式创建连接字符串,用于将实体框架代码优先模型与现有 Sql Server Compact 数据库映射

发布于 2024-11-18 11:09:10 字数 2168 浏览 1 评论 0原文

我已使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瞳孔里扚悲伤 2024-11-25 11:09:10

如果您首先使用 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 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.

浮生未歇 2024-11-25 11:09:10

打开 DbConnection 并将其传递给 DbContext 构造函数或使用 DefaultConnectionFactory:

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

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

SqlCeConnectionFactory 是 Microsoft 提供的现成的连接工厂,但您也可以实现自己的工厂。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文