Windows服务:EF + MySql没有app.config [英] Windows Service: EF + MySql without app.config

查看:424
本文介绍了Windows服务:EF + MySql没有app.config的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个Windows服务。



我已经在使用了完美的MSSQL数据库的EntityFramework。现在我必须平行使用MySql。但是我无法设法让它运行...我想避免使用app.config并通过从DbContext派生的类的构造函数配置EntityFramework。



我有一个SqlContext类:

  public class SqlContext:DbContext 
{
public IDbSet< ServiceauftragSource> ServiceauftragSource {get;组;

public SqlContext(string connectionString)
:base(connectionString)
{

}

public SqlContext(DbConnection连接)
:base(connection,true)
{
this.Configuration.LazyLoadingEnabled = true;
}
}

在我的UnitOfWork的构造函数中,我尝试创建我的SqlContext:

  public SqlUnitOfWork()
{
const string connStr =server = 127.0.0.1; UID =为myuser; PWD = mypw;数据库= MYDB;;
MySqlConnection conn = new MySqlConnection(connectionString);

this.Context = new SqlContext(conn);
}

这没有工作。尝试访问数据库时,我收到以下消息:

 无法确定DbProviderFactory类型连接类型'MySql.Data .MySqlClient.MySqlConnection。确保在应用程序配置中安装或注册了ADO.NET提供程序。 

也没有:

  public SqlUnitOfWork()
{
this.SetConnectionString();
this.Context = new SqlContext(connectionString);
}

private void SetConnectionString()
{
this.connectionString =Data Source =+ debugDatabaseServer +; Initial Catalog =+ debugDatabaseName
+; User ID =+ debugDatabaseUsername +; Password =+ debugDatabasePassword
+; Trusted_Connection = False; Persist Security Info = True;
}

我不知道为什么,但我认为这是因为我没有告诉我的上下文它的提供者(根据SO上的其他线程,它必须是MySql.Data.MySqlClient)。但是在哪里和如何?



项目的引用:






更新



我试图使用我的App.config:

 <?xml version =1.0编码= UTF-8 >?; 
< configuration>
< configSections>
<! - 有关实体框架配置的更多信息,请访问http://go.microsoft.com/fwlink/?LinkID=237468 - >
< section name =entityFrameworktype =System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection,EntityFramework,Version = 6.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089requirePermission =false/> ;
< / configSections>
< connectionStrings>
< add name =MySqlContextproviderName =MySql.Data.MySqlClientconnectionString =server = localhost;
port = 3306; database = ***; uid = ***; password = ***/>
< / connectionStrings>
< entityFramework>
< defaultConnectionFactory type =System.Data.Entity.Infrastructure.SqlConnectionFactory,EntityFramework/>
< providers>
< provider invariantName =MySql.Data.MySqlClienttype =MySql.Data.MySqlClient.MySqlProviderServices,
MySql.Data.Entity.EF6/>
< provider invariantName =System.Data.SqlClienttype =System.Data.Entity.SqlServer.SqlProviderServices,EntityFramework.SqlServer/>
< / providers>
< / entityFramework>
< / configuration>

访问数据库时会收到以下错误:

 对象引用未设置为对象的实例。 

我认为这是因为我的服务在安装后无法访问该app.config。我确保app.config在构建之后是MyService.exe.config ...

解决方案

我一直在使用MySQL与EF相当一段时间非常完美,妳需要做的是在DBContext上方添加以下行

  [DbConfigurationType(typeof(MySql .Data.Entity.MySqlEFConfiguration))] 

所以在你的情况下,代码片看起来像

  [DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))] 
public class SqlContext:DbContext
{
public IDbSet< ServiceauftragSource> ServiceauftragSource {get;组;

public SqlContext(string connectionString)
:base(connectionString)
{

}

public SqlContext(DbConnection连接)
:base(connection,true)
{
this.Configuration.LazyLoadingEnabled = true;
}
}

对于您的连接字符串,我倾向于存储我的在web配置,然后字符串格式为任何数据库,服务器或用户我需要创建一个上下文 - 希望有帮助!



NB我假设你有你的参考MySQL.data和EF6!


At the moment I'm writing a windows service.

I'm already using EntityFramework with MSSQL database which is working perfectly. Now I have to use MySql parallely. But I can't manage to get it running ... I would like to avoid using the app.config and configure EntityFramework via the constructor of my class derived from DbContext.

I have a SqlContext class:

public class SqlContext : DbContext
{
    public IDbSet<ServiceauftragSource> ServiceauftragSource { get; set; }

    public SqlContext(string connectionString)
        : base(connectionString)
    {

    }

    public SqlContext(DbConnection connection)
        : base(connection, true)
    {
        this.Configuration.LazyLoadingEnabled = true;
    }
}

In the constructor of my UnitOfWork I try to create my SqlContext:

    public SqlUnitOfWork()
    {
        const string connStr = "server=127.0.0.1;uid=myuser;pwd=mypw;database=mydb;";
        MySqlConnection conn = new MySqlConnection(connectionString);

        this.Context = new SqlContext(conn);
    }

This didn't work. I get the following message when trying to access the database:

Unable to determine the DbProviderFactory type for connection of type 'MySql.Data.MySqlClient.MySqlConnection'. Make sure that the ADO.NET provider is installed or registered in the application config.

Neither did:

    public SqlUnitOfWork()
    {
        this.SetConnectionString();
        this.Context = new SqlContext(connectionString);
    }

    private void SetConnectionString()
    {
        this.connectionString = "Data Source=" + debugDatabaseServer + ";Initial Catalog=" + debugDatabaseName 
                                        + ";User ID=" + debugDatabaseUsername + ";Password=" + debugDatabasePassword 
                                        + ";Trusted_Connection=False;Persist Security Info=True;";            
    }

I'm not sure why but I think this is because I haven't told my context its provider (according to other threads on SO it has to be MySql.Data.MySqlClient). But where and how to?

References of the project:


Update

I tried to use my App.config:

<?xml version="1.0" encoding="utf-8"?>
  <configuration>
    <configSections>
      <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
      <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </configSections>
    <connectionStrings>
      <add name="MySqlContext" providerName="MySql.Data.MySqlClient" connectionString="server=localhost;
        port=3306;database=***;uid=***;password=***"/>
    </connectionStrings>
    <entityFramework>
      <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
      <providers>
        <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, 
          MySql.Data.Entity.EF6" />
        <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      </providers>
    </entityFramework>
  </configuration>

When accessing the database I get the following error:

Object reference not set to an instance of an object.

I think this is because my service can't access the app.config after it's being installed. I ensured that app.config is MyService.exe.config after build ...

解决方案

I've been using MySQL with EF for quite some time pretty flawlessly, what yo need to do is add the following line above your DBContext

[DbConfigurationType(typeof (MySql.Data.Entity.MySqlEFConfiguration))]

so in your case the snippet would look like

 [DbConfigurationType(typeof (MySql.Data.Entity.MySqlEFConfiguration))]   
 public class SqlContext : DbContext
{
    public IDbSet<ServiceauftragSource> ServiceauftragSource { get; set; }

    public SqlContext(string connectionString)
        : base(connectionString)
    {

    }

    public SqlContext(DbConnection connection)
        : base(connection, true)
    {
        this.Configuration.LazyLoadingEnabled = true;
    }
}

as for your connection string, i tend to store mine in the web config and then string format for whatever db,server or user i need to create a context for - hope that helps!

NB Im presuming you have your refs to MySQL.data and EF6 as well!

这篇关于Windows服务:EF + MySql没有app.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆