Custom DbConfiguration Sample

Custom DbConfiguration Sample

Postby cangunaydin » Tue 25 Apr 2017 22:57

Hello,
I am using Entity Framework 6 with MySql Db. I have downloaded and install the trial version of dotconnect for mysql. Now i am trying to implement dotconnect to my project. I am using .net core cli to add migrations. I have referenced the Devart.Data.dll, Devart.Data.MySql.dll and Devart.Data.MySql.Entity.EF6.dll to my entityframework project. I have created custom dbconfiguration class which you can see below.

Code: Select all
[DbConfigurationType(typeof(BookAndAdDbConfiguration))]
    public class BookAndAdDbContext : AbpZeroDbContext<Tenant, Role, User>
    {
        /* Define an IDbSet for each entity of the application */

        public virtual IDbSet<BinaryObject> BinaryObjects { get; set; }

        public virtual IDbSet<Friendship> Friendships { get; set; }

        public virtual IDbSet<ChatMessage> ChatMessages { get; set; }

        public virtual IDbSet<Advertiser> Advertisers { get; set; }
        public virtual IDbSet<Agreement> Agreements { get; set; }

        public virtual IDbSet<ShareHolder> ShareHolders { get; set; }
        public virtual IDbSet<Offer> Offers { get; set; }
        public virtual IDbSet<OfferPdfFile> OfferPdfFiles { get; set; }
        public virtual IDbSet<Group> Groups { get; set; }
        public virtual IDbSet<MediaLength> MediaLengths { get; set; }
        public virtual IDbSet<RuleSet> RuleSets { get; set; }
        public virtual IDbSet<Screen> Screens { get; set; }
        public virtual IDbSet<Tag> Tags { get; set; }
        public virtual IDbSet<Seller> Sellers { get; set; }
        public virtual IDbSet<Availability> Availabilities { get; set; }


        public virtual IDbSet<Associate> Associates { get; set; }
        public virtual IDbSet<Address> Addresses { get; set; }

        public virtual IDbSet<Phone> Phones { get; set; }
        public virtual IDbSet<City> Cities { get; set; }
        public virtual IDbSet<Country> Countries { get; set; }

        public virtual IDbSet<BookedOffer> BookedOffers { get; set; }
        public virtual IDbSet<Playlist> Playlists { get; set; }
        public virtual IDbSet<Content> Contents { get; set; }

        public BookAndAdDbContext()
            : base(GetConnectionString())
        {

        }

        private static string GetConnectionString()
        {
            //Notice that; this logic only works on development time.
            //It is used to get connection string from appsettings.json in the Web project.

            var configuration = AppConfigurations.Get(
                WebContentDirectoryFinder.CalculateContentRootFolder()
                );

            return configuration.GetConnectionString(
                BookAndAdConsts.ConnectionStringName
                );
        }

        public BookAndAdDbContext(string nameOrConnectionString)
            : base(nameOrConnectionString)
        {

        }

        public BookAndAdDbContext(DbConnection existingConnection)
            : base(existingConnection, false)
        {

        }

        public BookAndAdDbContext(DbConnection existingConnection, bool contextOwnsConnection)
            : base(existingConnection, contextOwnsConnection)
        {

        }
    }

    public class BookAndAdDbConfiguration : DbConfiguration
    {
        public BookAndAdDbConfiguration()
        {
            //Mysql Implementation
            //SetProviderServices("MySql.Data.MySqlClient", new MySqlProviderServices());
            //SetDefaultConnectionFactory(new MySqlConnectionFactory());

            SetProviderServices("Devart.Data.MySql", Devart.Data.MySql.Entity.MySqlEntityProviderServices.Instance);
            SetProviderFactory("Devart.Data.MySql", Devart.Data.MySql.MySqlProviderFactory.Instance);
            //SetProviderServices(
            //    "System.Data.SqlClient",
            //    System.Data.Entity.SqlServer.SqlProviderServices.Instance
            //);
        }
    }



and add the sqlgenerator code to the configuration constructor.

Code: Select all
public Configuration()
        {
            //SetSqlGenerator("MySql.Data.MySqlClient", new MySql.Data.Entity.MySqlMigrationSqlGenerator());
            SetSqlGenerator(MySqlConnectionInfo.InvariantName, new MySqlEntityMigrationSqlGenerator());
            AutomaticMigrationsEnabled = false;
            ContextKey = "BookAndAd";
        }



When i try to add migration from command prompt with the command dotnet ef migrations add "initialize", it is saying "Keyword not supported: 'port'." It feels like it is still trying to use ms sql connector. What am i doing wrong? And if you have a sample about custom dbconfiguration it will be so helpful. Thank you for the assistance.
cangunaydin
 
Posts: 4
Joined: Tue 25 Apr 2017 22:34

Re: Custom DbConfiguration Sample

Postby Shalex » Wed 26 Apr 2017 17:07

Please give us the additional information:
1) specify the full stack trace of the error with all inner exceptions
2) localize the issue and send us a small test project for reproducing
Shalex
Devart Team
 
Posts: 7536
Joined: Thu 14 Aug 2008 12:44

Re: Custom DbConfiguration Sample

Postby cangunaydin » Thu 27 Apr 2017 05:55

Hello i have sent my project by email.
Thank you for your help.
cangunaydin
 
Posts: 4
Joined: Tue 25 Apr 2017 22:34

Re: Custom DbConfiguration Sample

Postby Shalex » Mon 01 May 2017 09:10

Thank you for the test project. We have reproduced the error and are investigating the issue.
Shalex
Devart Team
 
Posts: 7536
Joined: Thu 14 Aug 2008 12:44

Re: Custom DbConfiguration Sample

Postby Shalex » Wed 10 May 2017 13:26

Please open \Test\aspnet-core\src\ByteBrick.Test.EntityFramework\EntityFramework\TestDbContext.cs and replace
Code: Select all
  public TestDbContext(): base(GetConnectionString()) { }

with
Code: Select all
  public TestDbContext(): base(new MySqlConnection(GetConnectionString()), true) { }


This fixed the issue in our environment.

Is this solution suitable for you?
Shalex
Devart Team
 
Posts: 7536
Joined: Thu 14 Aug 2008 12:44


Return to dotConnect for MySQL