我目前正在使用控制台应用程序和类库进行测试。控制台应用程序对实体框架一无所知,只是调用类库中的方法。
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"; }
}
}
}
依赖解析器基于 Ryan Griffith 在 EricEJ 提到的链接中的回答。
这似乎在我的测试程序的上下文中工作,但我还没有在 Visual Studio 扩展的上下文中尝试过。
据我所知,只有在 DbConfiguration 首次在 AppDomain 中加载之前将事件处理程序添加到 DbConfiguration.Loaded 时(我可能无法保证),这种技术才有效。我还担心它可能会对 Visual Studio 或其他 Visual Studio 扩展产生副作用。