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.