Windows Service: EF + MySql without app.config

浪尽此生 提交于 2019-12-11 19:47:07

问题


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 ...


回答1:


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!



来源:https://stackoverflow.com/questions/23219981/windows-service-ef-mysql-without-app-config

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!