繁体   English   中英

没有配置文件的实体框架6 SQL Server Compact 3.5

[英]Entity Framework 6 SQL Server Compact 3.5 without Config file

我正在尝试使用EntityFramework.SqlServerCompact.Legacy 6.1.3使用Entity Framework 6访问SQL Server Compact 3.5数据库。

我的应用程序是Visual Studio扩展,无法将任何内容添加到应用程序配置文件中。

如果创建一个简单的测试应用程序,则可以毫无问题地连接到数据库。 如果我尝试从类库访问数据库,它将失败并显示错误消息

指定的架构无效。 错误:ProjectTemplate.ssdl(2,2):错误0152:未找到具有不变名称'System.Data.SqlServerCe.3.5'的ADO.NET提供程序的实体框架提供程序。 确保提供程序已在应用程序配置文件的“ entityFramework”部分中注册。 有关更多信息,请参见http://go.microsoft.com/fwlink/?LinkId=260882

我发现了一些有趣的链接,例如在这里此处,但是我无法使它正常工作,因为该示例适用于EF5或SQL Server(或者我不知所措)。

我该怎么做?

我目前正在使用控制台应用程序和类库进行测试。 控制台应用程序对Entity Framework一无所知,仅在类库中调用一个方法。

static void Main(string[] args)
{
  EFTest t = new EFTest();
  t.Test();
}

这是类库中的代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Core.EntityClient;

namespace EF6_SQLCompact_CleanTest_000
{
  public class EFTest
  {
    public void Test()
    {
      try
      {
        // Add an event handler for DbConfiguration.Loaded, which adds our dependency 
        // resolver class to the chain of resolvers.
        System.Data.Entity.DbConfiguration.Loaded += (_, a) => {
          a.AddDependencyResolver(new MyDependencyResolver(), true);
        };

        // Define the provider connection string, specifying the SQL Server Compact 3.5 filename.
        String FileName = @"f:\DotNetTestProjects\2015\CS\SQLCompact35DllTest\SQLCompact35DllTest\ProjectTemplate.sdf";
        String ConnectionString = String.Format("Data Source={0}", FileName);

        // Create the entity framework connection string, specifying the model,
        // the provider and the provider connection string.
        var builder = new EntityConnectionStringBuilder();
        builder.Metadata = @"res://*/ProjectTemplate.csdl|res://*/ProjectTemplate.ssdl|res://*/ProjectTemplate.msl";
        builder.Provider = "System.Data.SqlServerCe.3.5";
        builder.ProviderConnectionString = ConnectionString;

        // Create the entity framework kontext
        Entities Context = new Entities(builder.ToString());

        // Do a trivial query as a test.
        int i = Context.Languages.Count();
        MessageBox.Show(i.ToString());
      }
      catch (Exception e)
      {
        MessageBox.Show(e.Message);
      }
    }
  }

  // Define an additional constructor for the Entities class, so that we can 
  // pass the connection string into the base class DbContext.
  public partial class Entities : DbContext
  {
    public Entities(String ConnectionString)
      : base(ConnectionString)
    {
    }
  }

  class MyDependencyResolver : System.Data.Entity.Infrastructure.DependencyResolution.IDbDependencyResolver
  {
    public object GetService(Type type, object key)
    {
      // Output the service attempting to be resolved along with it's key 
      System.Diagnostics.Debug.WriteLine(string.Format("MyDependencyResolver.GetService({0}, {1})", type.Name, key == null ? "" : key.ToString()));

      if ((type == typeof(System.Data.Common.DbProviderFactory))
           && (key != null)
           && ((string)(key) == "System.Data.SqlServerCe.3.5"))
      {
        return System.Data.SqlServerCe.SqlCeProviderFactory.Instance ;
      }
      else if ((type == typeof(System.Data.Entity.Core.Common.DbProviderServices))
                && (key != null)
                && ((string)(key) == "System.Data.SqlServerCe.3.5"))
      {
        return System.Data.Entity.SqlServerCompact.Legacy.SqlCeProviderServices.Instance ;
      }
      else if ((type == typeof(System.Data.Entity.Infrastructure.IProviderInvariantName))
                && (key is System.Data.SqlServerCe.SqlCeProviderFactory))
      {
        return new MyProviderInvariantName();
      }

      return null;
    }

    public IEnumerable<object> GetServices(Type type, object key)
    {
      return new object[] { GetService(type, key) }.ToList().Where(o => o != null);
    }
  }

  // Implement IProviderInvariantName so that we can return an object when
  // requested in GetService()
  class MyProviderInvariantName : IProviderInvariantName
  {
    public string Name
    {
      get { return "System.Data.SqlServerCe.3.5"; }
    }
  }
}

依赖项解析器基于EricEJ提到的链接中Ryan Griffith的回答。

这似乎可以在我的测试程序的上下文中工作,但是我还没有在Visual Studio Extension的上下文中尝试过。

据我所知,仅当在AppDomain中首次加载DbConfiguration之前将事件处理程序添加到DbConfiguration.Loaded中时,此技术才有效(我可能无法保证)。 我也担心它可能会对Visual Studio或其他Visual Studio扩展产生副作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM