0

I want to try using Entity Framework without app/web.config. Thus I'd like to try out Entity Framework code based configuration. I have spent a while searching for help. Microsoft's own article here barely touches on the topic. Searching high and low on the web and I'm still none the wiser.

I want to configure EF to use a local Sql Express database. e.g. In My original app.config I have the following connection string.

<connectionStrings>
<add name="DataContextConnection" connectionString="Data Source=MYPC-L02\SQLEXPRESS2019;Database=EF_TestDb;Integrated Security=true" providerName="System.Data.SqlClient"/>

I'd like to convert that app.config above to code based config thus

public class MyDbConfiguration : DbConfiguration
{
    public MyDbConfiguration()
    {
        this.SetProviderServices("System.Data.SqlClient", System.Data.Entity.SqlServer.SqlProviderServices.Instance);

        //SetExecutionStrategy("System.Data.SqlClient", () => new DefaultExecutionStrategy());
        //SetDefaultConnectionFactory(new SqlConnectionFactory("Data Source=MYPC-L02\SQLEXPRESS2019;Database=EF_TestDb;Integrated Security=true"));
        SetDefaultConnectionFactory(new SqlConnectionFactory("Data Source=MYPC-L02\SQLEXPRESS2019;Initial Catalog=EF.TestDb;Integrated Security=True"));
    }
}

(you can see the sort of stuff I've been trying) and I apply this to my datacontext thus

[DbConfigurationType(typeof(MyDbConfiguration))]
public class DataContext : DbContext

but no matter what I try, I cant get the thing to work. It either errors, or comes up with its own connection string or what have you. I just want to configure the simplest data connection to a sql express database.

Can anyone help me with an example of what I need? or a link to a good resource ?

My thanks in advance.

3
  • Why not just use the DbContext constructor parameter to pass the connection string? Without the parameter, the DbContext will initialize using a connection string matching it's type. Alternatively you can pass it a connection string setting name to initialize from the config, or pass it an actual connection string you have built or sourced from somewhere else. Commented Feb 25, 2021 at 21:20
  • I personally haven't configured Entity Framework like this but I believe I found a similar question asked on here awhile ago with an answer/solution to the question. Hopefully this can help! Check out this link -> <stackoverflow.com/questions/38636508/…> A trending solution I've seen across multiple posts is passing the connection string into the DbContext constructor using the connection string either from the config file or sourced from another location. Commented Feb 25, 2021 at 21:46
  • Yes thanks folks. The DataContext has to have a default constructor fot code based config. I can maybe create a connection string builder and have that execute in the base class constructor, but why then am I using SqlConnectionFactory. I give that a connection string but why is it not being used. Also I use dependency injection and would dearly like to have my connection string available in a settings file (not app/web.config) but our own json settings file. Commented Feb 26, 2021 at 16:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.