Entity Framework 4.1 / 4.2 work arounds.

Keywords: Entity Framework not updating, Entity Framework not inserting,  Entity Framework not deleting.  When the default code doesn’t seem to do it for you there are a few work arounds you can try ot get connected with your DBContext or extended DBContext if using Code First.
* Make sure that you have the connection string in each app.config/web.config of every project you are using.
If you are using Code First, find your Context class and add a default contrustructor and one that takes a connection string like so: (From my OData / EF 4.2 /MVC 4 Demo)

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=DefaultConnectionconnectionString=Data Source=.\;InitialCatalog=CodeFirst;Integrated Security=True;User Instance=True;MultipleActiveResultSets=TrueproviderName=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=WGEEntitiesconnectionString=metadata=res://*/DataFirst.csdl|res://*/DataFirst.ssdl|res://*/DataFirst.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.;initial catalog=WGE;integrated security=True;multipleactiveresultsets=True;App=EntityFramework&quot;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 : ObjectContext

This 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.

Categories: Microsoft Integration Architects User Group

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: