public class OrderSystemContext : DbContext
{
//New Constructor Calling DBContext(connstring)
public OrderSystemContext(string connString) : base(connString){}
//Need to add a base constructor since you have a constructor with parameters.
public OrderSystemContext() : base(){}
publicDbSet<StateInfo> States { get; set; }
publicDbSet<Order> Orders { get; set; }
publicDbSet<Coupon> Coupons { get; set; }
publicDbSet<Promotion> Promotions { get; set; }
publicDbSet<Product> Products { get; set; }
publicDbSet<Customer> Customers { get; set; }
publicDbSet<Location> Locations { get; set; }
publicDbSet<ProductSet> ProductSets { get; set; }
}
Then you can directly call the connection string directly.
//This is calling the string by name, by default the “DefaultConnection” should work if places as a connection string name and you pass no parameters. Though it did not in my case.
using (var db = new OrderSystemContext(“DefaultConnection”))
{}
<connectionStrings>
<add name=“DefaultConnection“connectionString=“Data Source=.;InitialCatalog=CodeFirst;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True“providerName=“System.Data.SqlClient“ />
</connectionStrings>
If you are using Data First then you can just use the
using (var db = new MyEntities(System.Configuration.ConfigurationManager.ConnectionStrings[“MyEntities”].ConnectionString))
Connection String:
<add name=“WGEEntities“connectionString=“metadata=res://*/DataFirst.csdl|res://*/DataFirst.ssdl|res://*/DataFirst.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=WGE;integrated security=True;multipleactiveresultsets=True;App=EntityFramework"“providerName=“System.Data.EntityClient“ />
Where you will use the class that is decorated in the .cs under the self tracking entities .TT file and will be extended from DbContext or ObjectContext like so:
public partial class MyEntities : ObjectContextThis should force the connection.
I am unsure why I am having to do this work around, however it has helped others so I figured it was worth a post.
Share on Facebook