Skip to content

Conversation

DiogoBarros90
Copy link
Contributor

@DiogoBarros90 DiogoBarros90 commented Oct 27, 2017

This builder needed to be extended to allow port specification.

Implements #405

govorovvs added a commit to govorovvs/fluent-nhibernate that referenced this pull request Nov 16, 2017
@danilobreda
Copy link
Contributor

@DiogoBarros90 there is some tests if the password have spaces ?

On the SqlServer ConnectionString Builder i had a problem with it... i needed to add double quotes on the password or username if its with space.

@SkydivingAngel
Copy link

SkydivingAngel commented Nov 9, 2018

I made changes to the downloaded source code:

Changes made in MySQLConnectionStringBuilder Class:

Added:

	private string port = "3306";

Added:

	public MySQLConnectionStringBuilder Port(string port)
	{
		this.port = port;
		IsDirty = true;
		return this;
	}

Changed:

	protected internal override string Create()
	{
		var connectionString = base.Create();

		if (!string.IsNullOrEmpty(connectionString))
			return connectionString;

		var sb = new StringBuilder();

		sb.AppendFormat("Server={0};Database={1};User ID={2};Password={3};Port={4}", server, database, username, password, port);

		return sb.ToString();
	}

so it can be used this way:

	MySQLConfiguration connection01 = MySQLConfiguration.Standard
	.ConnectionString(c => c
	.Server("localhost")
	.Database(dbName)
	.Username("xxxx")
	.Password("yyyy")
	.Port("3308")
	)

Full Example:

public class SessionFactoryCreator
{
    public static ISessionFactory GetSession(string dbName = "Test", byte timeout = 10)
    {
        MySQLConfiguration connection = MySQLConfiguration.Standard
            .ConnectionString(c => c
                .Server("localhost")
                .Database(dbName)
                .Username("xxx")
                .Password("yyy")
                .Port("3306")
            )
            .ShowSql().FormatSql();

        ISessionFactory sessionFactory = Fluently.Configure()
            .Database(connection)
            .Cache(cacheBuilder =>
            {
                cacheBuilder.UseQueryCache();
                cacheBuilder.UseSecondLevelCache().ProviderClass<NHibernate.Cache.HashtableCacheProvider>();
            })
            .Mappings(mapper =>
            {
                mapper.FluentMappings.Add<FirstMap>();
                mapper.FluentMappings.Add<SecondMap>();
            })
            .ExposeConfiguration(cfg =>
            {
                cfg.SetProperty(NHibernate.Cfg.Environment.CommandTimeout, "180");
                cfg.SetProperty(NHibernate.Cfg.Environment.ReleaseConnections, "after_transaction");
                cfg.SetProperty(NHibernate.Cfg.Environment.BatchSize, "1000");
                new SchemaUpdate(cfg).Execute(false, true);
            })
            .BuildSessionFactory();

        return sessionFactory;
    }
}

@hazzik hazzik changed the title Changed the MySQL ConnectionString builder. Add Port toMySQL ConnectionString builder Sep 18, 2020
@hazzik hazzik changed the title Add Port toMySQL ConnectionString builder Add Port to MySQL ConnectionString builder Sep 18, 2020
@hazzik hazzik merged commit 8578d78 into nhibernate:master Sep 18, 2020
@hazzik hazzik added this to the 2.1.3 milestone Sep 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants