Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
376 views
in Technique[技术] by (71.8m points)

c# - EntityFramework 6 Code-based configuration of connection string for database-first

I'm attempting to make an existing application work without an app.config (it is required due to a very specific environment). Problem is that it's heavily relying on EntityFramework 6 to work with an SQL-Server.

I'm trying to use a code-based configuration, but I can't figure out how to provide a correct connection string through my configuration class.

I made a configuration class:

public class MyConfiguration : DbConfiguration
{
    public MyConfiguration()
    {
        SetDefaultConnectionFactory(new MyConnectionFactory());
        SetProviderServices("System.Data.SqlClient", System.Data.Entity.SqlServer.SqlProviderServices.Instance);
    }
}

Then provided it to my DbContext (Generated by EF automatically from bd):

[DbConfigurationType(typeof(MyConfiguration))]
public partial class TestModelEntities
{           
}

With a custom connection factory:

public class MyConnectionFactory : IDbConnectionFactory
{
    public DbConnection CreateConnection(string nameOrConnectionString)
    {
        var newConnStringBuilder = new SqlConnectionStringBuilder
        {
            UserID = "user",
            Password = "pass",
            InitialCatalog = "databaseName",
            DataSource = "serverName"
        };

        var entityConnectionBuilder = new EntityConnectionStringBuilder
        {
            Provider = "System.Data.SqlClient",
            ProviderConnectionString = newConnStringBuilder.ToString(),
            Metadata = @"res://*/TestModel.csdl|
                            res://*/TestModel.ssdl|
                            res://*/TestModel.msl"
        };

        var newDbConnect = new EntityConnection(entityConnectionBuilder.ToString());
        return newDbConnect;
    }
}

However. When I test it, I get an UnintentionalCodeFirstException. Why? What am I missing?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should provide connection string to your context via :base(connectionString). Create a class as below:

public class ConnectionStringBuilder
{
    public static string Construct()
    {
        var newConnStringBuilder = new SqlConnectionStringBuilder
        {
            UserID = "user",
            Password = "pass",
            InitialCatalog = "databaseName",
            DataSource = "serverName"
        };

        var entityConnectionBuilder = new EntityConnectionStringBuilder
        {
            Provider = "System.Data.SqlClient",
            ProviderConnectionString = newConnStringBuilder.ToString(),
            Metadata = @"res://*/TestModel.csdl|
                            res://*/TestModel.ssdl|
                            res://*/TestModel.msl"
        };

        return entityConnectionBuilder.ToString();
    }
}

Then modify your Context constructor to look like this:

public DbContext()
    : base(ConnectionStringBuilder.Construct())
{
}

It should work fine now. (source)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...