Renato Xavier
Renato Xavier

Reputation: 123

CodeFluent Entities - help setting up MySql consumption at design time in Visual Studio

I have an issue in Visual Studio 2013, when consuming the object model generated by CodeFluent Entities at DESIGN TIME.

It's a 3rd party reporting component that enables binding to POCO object datasources, with the added functionality of delivering live data from the returned collection of objects provided by the POCO datasource. It then shows a nice preview of report with data, inside Visual Studio 2013 at DESIGN TIME.

I set up a report pointing to an object datasource where the return type is CodeFluent Entities "Namespace"."Entity"Collection type (I just use the LoadAll function generated by default and return its call).

Well then, I can make this work fine. At DESIGN TIME, the reporting component in Visual Studio correctly calls the LoadAll method in the BOM and retrieves data from the persistence layer. All this in DESIGN TIME (no project in my VS solution is ever run to get LoadAll fired and retrieving data from the database), I cannot stress this enough.

This is where I need your help. What I described works when consuming SQL Server as a persistence layer, but NOT when consuming MySql.

Since all the functionality I'm describing here is at VS design time, this report component uses a special configuration to instantiate .NET classes and run methods from the object, without ever having the project entry point run. Since project is never run, it does not fire the initialization code for CodeFluent Entities classes to consume the persistence layer properly. I have then to make a new class, with a new function where the connection string in CodeFluent.Runtime.CodeFluentContext is set from scratch.

In practical terms, this is the issue:

I have a "special" class with a function called by its constructor to serve as the object datasource for the reporting component, such as:

Public Class DuplicataLoadService
    Public Function LoadByParams() As S5T.DuplicataCollection
        Dim context As CodeFluentContext = CodeFluent.Runtime.CodeFluentContext.[Get](S5T.Constants.S5TStoreName)

        context.Persistence.ConnectionString = "Data Source=MYCOMPUTER\SQLEXPRESS;Initial Catalog=duplicatas4t;User ID=user;Password=pwd"

        LoadByParametros = S5T.DuplicataCollection.LoadAll
    End Function

    Public Sub New()
        Me.LoadByParams()
    End Sub

End Class

I call this class "special", because it is never executed at runtime, by starting my winforms project. I set breakpoints on it they are never hit. This class is used in Visual Studio DESIGN TIME only.

My App.config, is set to consume a SQL Server persistence, such as:

<configuration>
    <configSections>
      <section name="S5T" type="CodeFluent.Runtime.CodeFluentConfigurationSectionHandler, CodeFluent.Runtime" />
    </configSections>

  <S5T connectionString="Data Source=MYCOMPUTER\SQLEXPRESS;Initial Catalog=duplicatas4t;User ID=user;Password=pwd" />

  </configuration>

In this scenario it works fine, but when I change to consume MySql persistence, it does not work anymore (please help).

** NOT WORKING, generic exception thrown ***

Public Class DuplicataLoadService
    Public Function LoadByParams() As S5T.DuplicataCollection
        Dim context As CodeFluentContext = CodeFluent.Runtime.CodeFluentContext.[Get](S5T.Constants.S5TStoreName)

        context.Persistence.ConnectionString = "Database=duplicatas4t;Server=localhost;Password=manager;User ID=root;pooling=true;"

        LoadByParametros = S5T.DuplicataCollection.LoadAll
    End Function

    Public Sub New()
        Me.LoadByParams()
    End Sub

End Class

My App.config, is set to consume a MySql persistence, such as:

<configuration>
    <configSections>
      <section name="S5T" type="CodeFluent.Runtime.CodeFluentConfigurationSectionHandler, CodeFluent.Runtime" />
    </configSections>

  <S5T persistenceTypeName="MySQL"
               connectionString="Database=duplicatas4t;Server=localhost;Password=pwd;User ID=user;pooling=true;"
               mysql-useSchemas="true" />

  </configuration>

In these both cases I mentioned app.config as an ILLUSTRATION only, because the app.config contents are set up and fetched at runtime when the project execution fires. This is not the case, I used the app.config contents only as a model so that I could know what would have to be programmatically set for the design time funcionality of consuming the persistence layer to work.

I suspect that I would have to programmatically add further configuration when it's MySql.

Since SQL Server is default for CodeFluent Entities, when the app.config has only:

<S5T connectionString......

It works, I have a corresponding code to set the connection string programmatically such as:

context.Persistence.ConnectionString = ....

When it is MySql, there are additional attributes present in app.config, besides connectionString, such as:

<S5T persistenceTypeName="MySQL".......  mysql-useSchemas="true" />

My initialization code being only context.Persistence.ConnectionString = .... is not enough.... Does it make sense?

How could I programmatically initialize persistenceTypeName and mysql-useSchemas attributes ?

As a final observation, both cases consuming SQL Server and MySql work normally at runtime, indicating that I don't have a basic configuration error when setting up the project's app.config, persistence producers, etc....

Thanks in advance,

Renato

Upvotes: 1

Views: 83

Answers (1)

meziantou
meziantou

Reputation: 21377

In the configuration file, you set the PersistenceTypeName and some specific MySQL configuration attributes. By default the PersistenceTypeName is set to SQL Server and the default connection string is used. This is why it works at design time with SQL Server.

When you use MySQL, you need to change the configuration (PersistenceTypeName, Connection String, etc.). As you cannot use the configuration file at design time, you must set the configuration by code. The CodeFluentConfiguration instance is available in the context:

var context = CodeFluent.Runtime.CodeFluentContext.Get("S5T");
context.Configuration.PersistenceTypeName = "MySQL";
context.Configuration.Element.SetAttribute("mysql-UseSchema", "true");

Provider specific attributes must be set in the XML element as there is no associated property in the generic configuration class.

Upvotes: 0

Related Questions