Quantcast
Channel: entityframework Discussions Rss Feed
Viewing all articles
Browse latest Browse all 1793

New Post: EF & Mono under Linux

$
0
0
Hi Liviu,

This is an interesting find. I wonder if this is reproducible without EF. Can you try the following snippet and let me know?
using (var connection = new SqlConnection(connString))
{
    connection.Open();

    foreach (var cmdText in new[] {"select serverproperty('EngineEdition')", "SELECT 1"})
    {
        using (var command = new SqlCommand(cmdText, connection))
        {
            using (var reader = command.ExecuteReader())
            {
                reader.Read();
                Console.WriteLine(reader.GetInt32(0));
            }
        }
    }
}
This is basically what EF does to detect if we are running against Sql Azure which has some requirements the on premise Sql Server versions don't have. If you are able to repro the problem with the above snippet it would mean that the bug is somewhere between Mono and SqlServer or is a result of a specific SqlServer configuration/security settings (I am not an expert in this area so probably won't be able to help much beyond this)

Another good news is that to workaround the problem you don't actually have to change the EF code. EF6 allows configuring services and one of the services allows overriding the default behavior of getting the provider manifest token. You can register your custom manifest token resolver using code based configuration and either return a hardcoded value (e.g. if you are running always against SqlServer 2012 you would just return "2012") or come up with the version on your own. This should prevent EF from checking what database version your app is running against. If you run against different versions of Sql Server you can use DbConnection.ServerVersion property to create a provider manifest token for your database. Here is an example - you can just copy/paste and it should work because EF should detect the configuration automatically (as long as the DbConfiguration derived class is in the same assembly as your context)
public class Configuration : DbConfiguration
{
    public Configuration()
    {
        SetManifestTokenResolver(new ManifestTokenResolver());
    }
}

public class ManifestTokenResolver : IManifestTokenResolver
{
    public string ResolveManifestToken(DbConnection connection)
    {
        // The simplest thing is just to return hardcoded value
        // return "2012";

        try
        {
            connection.Open();

            var majorVersion =
                Int32.Parse(connection.ServerVersion.Substring(0, 2), CultureInfo.InvariantCulture);

            if (majorVersion == 9)
            {
                return "2005";
            }

            if (majorVersion == 10)
            {
                return "2008";
            }

            return "2012";

        }
        finally 
        {
            if (connection.State == ConnectionState.Open)
            {
                connection.Close();
            }
        }
    }
}
Thanks,
Pawel

Viewing all articles
Browse latest Browse all 1793

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>