如何在代码中定义 Entity Framework 提供程序?

How do I define Entity Framework provider in code?

我正在使用JetEntityFrameworkProvider。我正在尝试连接到一个 MS Access 文件(它有分机 .sep 但它确实是一个 Access 文件)。

我试图在代码中定义连接字符串和提供程序,但它不起作用。在 运行 之前,我收到以下错误:

Unable to determine the provider name for provider factory of type 'JetEntityFrameworkProvider.JetProviderFactory'. Make sure that the ADO.NET provider is installed or registered in the application config.

我不想在配置中配置提供程序。当然有办法做到这一点。

当我执行 运行 时(是的,它会构建),我收到此错误:

System.InvalidOperationException: 'The 'Jet OLEDB:Database' provider is not registered on the local machine.'

上下文 class

public class ProjectContext : DbContext
{
    private DbConnection con = new JetConnection();

    public ProjectContext() : base(new JetConnection(""Provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:\Test-Project.sep'; Provider=Jet OLEDB:Database; Password=SEEME;""), true)
    {
    }

    public DbSet<Component> Components { get; set; }
}

实体class

public class Component
{
    [Key]
    [Column("Counter")]
    public int Id { get; set; }
    [Column("Name")]
    public string Name { get; set; }
}

我通过将连接字符串更改为此解决了这个问题。

public ProjectContext() : base(new JetConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='C:\Test-Project.sep'; providerName=JetEntityFrameworkProvider; Password=SEEME;"), true)
{

}

但是,我有一个新问题和一个新错误,所以我会 post 一个新问题。