RajasekharwhY / Build-your-muscle

Learning concepts and sample practice code - C#, MVC, SQL etc..
2 stars 0 forks source link

dbContext, datacontext and Objectcontext #16

Open RajasekharwhY opened 5 years ago

RajasekharwhY commented 5 years ago

What is the difference between System.Data.Linq.DataContext and System.Data.Entity.DbContext? Basically this stems from two different technologies, LINQ-to-SQL and Entity Framework. The first assembly reference, System.Data.Linq.DataContext originates from LINQ-to-SQL and was used as a method for interacting with databases. The System.Data.Entity.DbContext is the newer assembly that is being used and stems from Entity Framework (which explains the "Entity" in the title). They are basically both handling the same basic logic between two different ORMs (LINQ-to-SQL and Entity Framework) although the syntax may vary slightly.

Entity Framework is typically included in just about every "new" project that is built through Visual Studio and has been adopted heavily in favor of LINQ-to-SQL. This probably explains why your older Silverlight project included the older references and assemblies that related to LINQ-to-SQL.

Datacontext or ObjectContext are keeps track of all entities instantiates. DbContext in Entity Framework is responsible for tracking the changes made on the entity or object, so the correct update is done to the database when the SaveChange() method of context is called. When we retrieve entities using an object query, the Entity Framework puts these entities in a cache and tracks whatever changes are made on these entities until the savechanges method is called. Entity Framework tracks the query results that return entity types.