{"id":95,"date":"2014-08-04T11:55:03","date_gmt":"2014-08-04T11:55:03","guid":{"rendered":"http:\/\/mairwa.com\/wordpress\/?p=95"},"modified":"2014-08-04T11:55:03","modified_gmt":"2014-08-04T11:55:03","slug":"entity-framework-questions","status":"publish","type":"post","link":"http:\/\/mairwa.com\/wordpress\/?p=95","title":{"rendered":"Entity framework questions"},"content":{"rendered":"<p><b>1) What is the advantage and disadvantage of Entity Framework<\/b><\/p>\n<p><b>Advantages<\/b>:<\/p>\n<p>One common syntax (LINQ) for all object queries whether it is database or not , Pretty fast if used as intended , easy to implement SoC , less coding required to accomplish complex tasks.<\/p>\n<p>Its fast and straight forward using LINQ\/FE objects For Add\/Modify\/Delete\/Update.<\/p>\n<p>Easy to map business objects (with drag &amp; drop tables on environment). It keeps a good performance when work with a small \/ middle domain model.<\/p>\n<p><b>Disadvantages<\/b>:<\/p>\n<p>we have to think in a non-traditional way of handling data , not available for every database.<\/p>\n<p>If there is any schema change in database FE won\u2019t work. We have to update the schema in solution as well!!!<\/p>\n<p>&nbsp;<\/p>\n<p>It&#8217;s limited when we work with a huge domain model. -Scalability.<\/p>\n<p><b>2) When should use EF and when we should not use EF.<\/b><\/p>\n<p>EF can&#8217;t handle more if number of datatable roughly maximum 250. whenever, if we have more than 250 tables, e.g. 1000+, I think break up 1000+ tables into several logical groups of tables, and use one EF model per such logical group (with up to 250 tables in it). If we do not want to have several model in a project, then 1000+ tables in one model is not so good.<\/p>\n<p><b>3) What would be the performance of the EF when we do huge data fetching or updating or inserting etc.<\/b><\/p>\n<ul>\n<li>EF performs slower when it has to populate an Entity with deep hierarchy. Be extremely careful with entities with deep hierarchy<\/li>\n<li>Indexing of database is good but in case of EF it becomes very important. The columns use for retrieval and sorting should be properly indexed.<\/li>\n<li>When the model is large, VS2010\/VS2012 Model designer gets real crazy. so break the model into medium sized models. There is a limitation that the Entities from different models cannot be shared even though they may be pointing to the same table in the database.<\/li>\n<li>Transactions are slow. be careful with them<\/li>\n<\/ul>\n<p><strong>4) <\/strong><b>Why should not use Entity Framework or an ORM?<\/b><\/p>\n<ul>\n<li>Bulk updates (in addition to bulk inserts)<\/li>\n<li>Querying across multiple databases: EF currently restricts the entities in a single context to a single database. If you are forced to work with a multi-database setup, this could be an issue.<\/li>\n<li>Lack of support for nullable keys: if you are dealing with a legacy data model where some tables have unique clustered indices with nullable columns, you will find EF unable to deal with this<\/li>\n<\/ul>\n<p><strong>5)What is the importance of EDMX file in Entity Framework?<\/strong><\/p>\n<p><b><a href=\"http:\/\/mairwa.com\/wordpress\/wp-content\/uploads\/2014\/08\/Untitled.gif\"><img loading=\"lazy\" decoding=\"async\" style=\"width:407px;height:58px;\" alt=\"Untitled\" src=\"http:\/\/mairwa.com\/wordpress\/wp-content\/uploads\/2014\/08\/Untitled-300x58.gif\" width=\"300\" height=\"58\" \/><\/a><\/b><\/p>\n<p>&nbsp;<\/p>\n<p>EDMX (Entity Data Model XML) is an XML file which contains all the mapping details of how the objects map with SQL tables. The EDMX file is further divided into three sections: CSDL, SSDL, and MSL.<\/p>\n<p><strong>6)Explain CSDL, SSDL and MSL sections in an EDMX file?<\/strong><\/p>\n<ul>\n<li>CSDL (Conceptual Schema definition language) is the conceptual abstraction which is exposed to the application.<\/li>\n<li>SSDL (Storage Schema Definition Language) defines the mapping with your RDBMS data structure.<\/li>\n<li>MSL (Mapping Schema Language) connects the CSDL and SSDL.<\/li>\n<\/ul>\n<p>CSDL, SSDL and MSL are actually XML files.<\/p>\n<p><a href=\"http:\/\/mairwa.com\/wordpress\/wp-content\/uploads\/2014\/08\/Untitled2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter  wp-image-107\" style=\"width:790px;height:171px;\" alt=\"Untitled\" src=\"http:\/\/mairwa.com\/wordpress\/wp-content\/uploads\/2014\/08\/Untitled2.png\" width=\"790\" height=\"210\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><strong>7) What are T4 templates?<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>T4 (Text Template Transformation Toolkit) is a template based code generation engine. To \u00a0write C# code in T4 templates (<em>.tt<\/em> is the extension) files and those C# codes execute to generate the file as per the written C# logic.<\/p>\n<p>For instance, the below T4 C# code:<\/p>\n<p>&lt;#@ template language=&#8221;\u201cC#\u201d&#8221; #&gt;<\/p>\n<p>Hello &lt;# Write(\u201dWorld!\u201d) #&gt;<\/p>\n<p>Will generate the following C# output:<\/p>\n<p>Hello<\/p>\n<p>World !<\/p>\n<p><b>Importance Of T4<\/b><\/p>\n<p><b>\u00a0 <\/b>It is EF code generation. The t4 code templates read the Edmx file and generate c# code behind. The c# code<\/p>\n<p><a href=\"http:\/\/mairwa.com\/wordpress\/wp-content\/uploads\/2014\/08\/Untitled3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-109\" alt=\"Untitled\" src=\"http:\/\/mairwa.com\/wordpress\/wp-content\/uploads\/2014\/08\/Untitled3.png\" width=\"808\" height=\"143\" \/><\/a><\/p>\n<p><strong>8)How to read records using Entity framework?<\/strong><\/p>\n<p>We create the object of context class and inside this context class will get the records.<\/p>\n<p>Ex\u2014customercontext custe=new customercontext();<\/p>\n<p>Foreach(cusromer objcust in custe.customers){}<\/p>\n<p>9)People say Entity Framework runs slow<\/p>\n<p>By default EF has lazy loading behavior. Due to this default behavior if we are loading a large number of records and especially if they have foreign key relationships, we can have performance issues. So we need to be cautious if really need lazy loading behavior for all scenarios. For better performance, disable lazy loading when we are loading a large number of records or use stored procedures.<\/p>\n<p>&nbsp;<\/p>\n<p><b>10)Can you explain lazy loading in a detailed manner?<\/b><\/p>\n<p>Lazy loading is a concept where we load objects on demand rather than loading everything in one go. Consider a situation where we have 1 to many relationships between the Customer and Address objects. Now let\u2019s say we are browsing the customer data but we do not want address data to be loaded at that moment. But the time we start accessing the address object we would like to load address data from the database.<\/p>\n<p>Entity Framework has lazy loading behavior by default enabled. For instance, consider the below code. When we are doing a <code>foreach<\/code> on the Customer object, the Address object is not loaded. But the time we start doing <code>foreach<\/code> on the address collection, the Address object is loaded from SQL Server by firing SQL queries.<\/p>\n<p>So in simple words, it will fire a separate query for each address record of the customer, which is definitely not good for a large number of records.<\/p>\n<p><sup>MyEntities context = new MyEntities();<\/sup><\/p>\n<p><sup>\u00a0<\/sup><\/p>\n<p><sup>var<\/sup><sup> Customers = context.Customers.ToList();<\/sup><\/p>\n<p><sup>\u00a0<\/sup><\/p>\n<p><sup>foreach<\/sup><sup> (Customercust in Customers) \/\/ In this line no address object loaded<\/sup><\/p>\n<p><sup>{<\/sup><\/p>\n<p><sup>\u00a0\u00a0\u00a0\u00a0 foreach(Address add in cust.Addresses){}\/\/ Address object is loaded here<\/sup><\/p>\n<p><sup>}<\/sup><\/p>\n<p><sup>\u00a0<\/sup><\/p>\n<p><b>11)How can we turn off lazy loading?<\/b><\/p>\n<p>The opposite of lazy loading is eager loading. In eager loading we load the objects beforehand. So the first thing is we need to disable lazy loading by setting <code>LazyLoadingEnabled<\/code> to <code>false<\/code>.<\/p>\n<ul>\n<li>context.ContextOptions.LazyLoadingEnabled = false;<\/li>\n<\/ul>\n<p>Now we have to explicitly tell EF what objects we want to load by using the <code>include<\/code> function. Below is a simple sample code where we tell EF to load customer as well as address objects by using the <code>include<\/code> function.<\/p>\n<p>Now the customer object and the related address objects will be loaded in one query rather than multiple queries.<\/p>\n<ul>\n<li>var employees = context.Customers.Include(&#8220;Addresses&#8221;).Take(5);<\/li>\n<\/ul>\n<p><b>12)What are POCO classes in Entity Framework?<\/b><\/p>\n<p>POCO means Plain Old C# Object. When EDMX creates classes, they are cluttered with a lot of entity tags. For instance, below is a simple customer class generated using Entity Framework. Many times we would like to use simple .NET classes and integrate them with Entity Framework.<\/p>\n<p>Entity Framework allows this. In other words you can create a simple .NET class and use the entity context object to load your simple .NET classes.<\/p>\n<p>Below is a simple class generated by EF which is cluttered with a lot of EF attributes.<\/p>\n<p>[EdmEntityTypeAttribute(NamespaceName=&#8221;CustomermytestModel&#8221;, Name=&#8221;Customer&#8221;)]<\/p>\n<p>[Serializable()]<\/p>\n<p>[DataContractAttribute(IsReference=true)]<\/p>\n<p>public partial class Customer : EntityObject<\/p>\n<p>{<\/p>\n<p>#region Factory Method<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/\/ <i>&lt;summary&gt;<\/i><\/p>\n<p>\/\/\/ Create a new Customer object.<\/p>\n<p>\/\/\/ <i>&lt;\/summary&gt;<\/i><\/p>\n<p>\/\/\/ <i>&lt;param<\/i> <i>name=&#8221;id&#8221;<\/i> <i>\/&gt;<\/i>Initial value of the Id property.<\/p>\n<p>\/\/\/ <i>&lt;param<\/i> <i>name=&#8221;customerCode&#8221;<\/i> <i>\/&gt;<\/i>Initial value of the CustomerCode property.<\/p>\n<p>\/\/\/ <i>&lt;param<\/i> <i>name=&#8221;customername&#8221;<\/i> <i>\/&gt;<\/i>Initial value of the Customername property.<\/p>\n<p>public static Customer CreateCustomer(global::System.Int32 id,<\/p>\n<p>global::System.String customerCode, global::System.String customername)<\/p>\n<p>{<\/p>\n<p>Customer customer = new Customer();<\/p>\n<p>customer.Id = id;<\/p>\n<p>customer.CustomerCode = customerCode;<\/p>\n<p>customer.Customername = customername;<\/p>\n<p>return customer;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>#endregion<\/p>\n<p>#region Primitive Properties<\/p>\n<p><b>13)How do we implement POCO in Entity Framework?<\/b><\/p>\n<p>To implement POCO is a three step process:<\/p>\n<p>Go to the designer and set the code generation strategy to <code>NONE<\/code>. This step means that we would be generating the classes on own rather than relying on EF auto code generation.<\/p>\n<p>Now that we have stopped the auto generation of code, we need to create the domain classes manually. Add a class file and create the domain classes like the <code>Customer<\/code> class we created.<\/p>\n<p>public class Customer<\/p>\n<p>{<\/p>\n<p>private string _customerName;<\/p>\n<p>&nbsp;<\/p>\n<p>public string CustomerName<\/p>\n<p>{<\/p>\n<p>get { return _customerName; }<\/p>\n<p>set { _customerName = value; }<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>private int _Customerid;<\/p>\n<p>&nbsp;<\/p>\n<p>public int Customerid<\/p>\n<p>{<\/p>\n<p>get { return _Customerid; }<\/p>\n<p>set { _Customerid = value; }<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<ul>\n<li>Write your Context layer code inheriting from <code>ObjectContext<\/code>. Disable auto-generation.<\/li>\n<\/ul>\n<p>public partial class Test123Entities : ObjectContext<\/p>\n<p>{<\/p>\n<p>public Test123Entities()<\/p>\n<p>: base(&#8220;name=Test123Entities&#8221;, &#8220;Test123Entities&#8221;)<\/p>\n<p>{<\/p>\n<p>this.ContextOptions.LazyLoadingEnabled = true;<\/p>\n<p>OnContextCreated();<\/p>\n<p>}<\/p>\n<p>partial void OnContextCreated();<\/p>\n<p>public ObjectSet&lt;Customer&gt; Customers<\/p>\n<p>{<\/p>\n<p>get<\/p>\n<p>{<\/p>\n<p>if ((_Customers == null))<\/p>\n<p>{<\/p>\n<p>_Customers = base.CreateObjectSet&lt;Customer&gt;(&#8220;Customers&#8221;);<\/p>\n<p>}<\/p>\n<p>return _Customers;<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>private ObjectSet&lt;Customer&gt; _Customers;<\/p>\n<p>public void AddToCustomers(Customer customer)<\/p>\n<p>{<\/p>\n<p>base.AddObject(&#8220;Customers&#8221;, customer);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>And finally we can use the above code in client as if where using EF normally.<\/p>\n<p>Test123Entities oContext = new Test123Entities();<\/p>\n<p>List&lt;Customer&gt; oCustomers = oContext.Customers.ToList&lt;Customer&gt;();<\/p>\n<p><b>14)In POCO classes do we need EDMX files?<\/b><\/p>\n<p>Yes, still need EDMX files because the context object reads the EDMX files to do the mapping.<\/p>\n<p><b>15)What is Code First approach in Entity Framework?<\/b><\/p>\n<p>In Code First approach we avoid working with the Visual Designer of Entity Framework. In other words the EDMX file is excluded from the solution. So now have complete control over the context class as well as the entity classes.<\/p>\n<p><b>16)What is the difference between POCO, Code First, and simple EF approach?<\/b><\/p>\n<p>All these three approaches define how much control you want on your Entity Framework code. Entity Framework is an OR mapper, it generates a lot of code, it creates middle tier (Entity), and Data Access layer (Context).<\/p>\n<p>But a lot of times we want to enjoy the benefits of both worlds, we want the auto-generation part to minimize development time and also want control on the code so that we can maintain code quality.<\/p>\n<p>Below is the difference table which defines each of the approaches. In simple Entity Framework, everything is auto generated and so we need the EDMX XML file as well. POCO is semi-automatic so we have full control on the entity classes but then the context classes are still generated by the EDMX file.<\/p>\n<p>In Code First, you have complete control on how you can create the entity and context classes. Because you are going to manually create these classes, you do not have dependency on the EDMX XML file. Below is a simple table which shows the cross comparison.<\/p>\n<table style=\"width:684px;height:210px;\" width=\"684\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<thead>\n<tr>\n<td width=\"181\"><\/td>\n<td width=\"78\"><strong>EDMX<\/strong><b><\/b><\/td>\n<td><strong>Entity<\/strong><b><\/b><\/td>\n<td width=\"87\"><strong>Context<\/strong><b><\/b><\/td>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td width=\"181\"><strong>Simple entity framework<\/strong><\/td>\n<td width=\"78\">Needed<\/td>\n<td>Auto<\/td>\n<td width=\"87\">Auto<\/td>\n<\/tr>\n<tr>\n<td width=\"181\"><strong>POCO approach<\/strong><\/td>\n<td width=\"78\">Needed<\/td>\n<td>Manual<\/td>\n<td width=\"87\">Auto<\/td>\n<\/tr>\n<tr>\n<td width=\"181\"><strong>Code First<\/strong><\/td>\n<td width=\"78\">Not Needed<\/td>\n<td>Manual<\/td>\n<td width=\"87\">Manual<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>17)<b>What is the difference between DbContext and ObjectContext?<\/b><\/p>\n<p><code>DbContext<\/code> is a wrapper around <code>ObjectContext<\/code>, it\u2019s a simplified version of <code>ObjectContext<\/code>.<\/p>\n<p align=\"center\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-115\" alt=\"Untitled\" src=\"http:\/\/mairwa.com\/wordpress\/wp-content\/uploads\/2014\/08\/Untitled4.png\" width=\"409\" height=\"185\" \/><\/p>\n<p>As a developer you can start with <code>DbContext<\/code> as it\u2019s simple to use. When you feel that some of the operations cannot be achieved by <code>DbContext<\/code>, you can then access <code>ObjectContext<\/code> from DbC<code>o<\/code>ntext, as shown in the below code:<\/p>\n<p>((IObjectContextAdapter)dbContext).ObjectContext<\/p>\n<p>&nbsp;<\/p>\n<p><b>18) What is LINQ?<\/b><\/p>\n<p>Language-Integrated Query (LINQ) is a way to query data without cumbersome stored procedures. Previously, programmers needed to create stored procedures and then call these stored procedures from their code. With Entity framework, we can pull data and query it using language similar to SQL.<\/p>\n<p><b>19) How is data retrieved?<\/b><\/p>\n<p>The difference between older retrieval methods and current Entity framework retrieval methods is that we can now (with Entity) retrieve data as objects. The objects represent the tables (or linked tables) in database. Instead of iterating through several columns and rows, you just use your class data models. For instance, if we have a table named \u201cusers,\u201d we can use the \u201cusers\u201d class instead of working through each data set after your query.<\/p>\n<p><b>20) Can we run SQL statements in an Entity framework environment?<\/b><\/p>\n<p>Yes, we can also run SQL query statements. use the \u201c<b>ExecuteStoreCommand<\/b>\u201d method to run SQL on your database. This is usually a secondary option from running simple LINQ on your Entity framework code. we can also run stored procedures from a database.<\/p>\n<p><b>21) How do you create a database model?<\/b><\/p>\n<p>Visual Studio has a database modeler available. we can create a database model from scratch, or we can query the database for the models. If we have a database already, you simply pull the database structures from your code and Entity framework will automatically set up the class data models.<\/p>\n<p><b>22) Does Entity framework support primary and foreign keys?<\/b><\/p>\n<p>Yes, Entity framework supports both types of primary and foreign keys. we can define these in the database tables and import them to your model classes. If don\u2019t already have a database set up, we can create these keys in the data model classes and their respective data modeling classes.<\/p>\n<p><b>23) How do you mark a data column as required by the database?<\/b><\/p>\n<p>we can \u201cdecorate\u201d the data models. The \u201cRequired\u201d decoration marks a field as required. Before a user can submit the data to the database, the user must have this field entered. we can also auto-generate required fields in the \u00a0code, so the code automatically adds the required data.<\/p>\n<p><b>24) What is lazy loading?<\/b><\/p>\n<p>Lazy loading is a way to return only objects that are used. When we query from the database model, lazy loading only returns the immediate tables needed by the user. All related tables are returned when they are used. This means we can reserve memory and storage when work with large programs. It also means that objects are created until need them, so it makes your program faster.<\/p>\n<p><b>25) What is eager loading?<\/b><\/p>\n<p>Eager loading is the opposite of lazy loading. When we query for objects, eager loading returns all of the objects including the related objects. For instance, when we \u00a0query a list of customers and orders, eager loading loads all objects including the customers and the orders instead of just what you originally need (customers).<\/p>\n<p><b>26) What is a navigation property?<\/b><\/p>\n<p>A navigation property points to related tables in the database model. For instance, if we have a customer table that relates to the orders table, the navigation property points from the customers table to the orders table. While the primary and foreign keys are physical properties of the table, the navigation properties are a logical part of a data model. When we view the Entity framework model, we can view the navigation properties to better understand the structure of your tables.<\/p>\n<p><b>27) What is a scalar property in your data model?<\/b><\/p>\n<p>A scalar property is similar to a scalar property in the database. A scalar property points to one location in the table.<\/p>\n<p><b>28) What is a complex data type?<\/b><\/p>\n<p>A complex data type occurs when we need to point to another data object from one data object. For instance, if we have several tables that require an address, we can turn those addresses into a table of addresses. then point the \u00a0address columns to the new address table, which creates a complex data type. You will likely have complex data types in every .NET Entity framework project, because it makes it easier to relate one table and data model to another without creating cumbersome code.<\/p>\n<p><b>29) Overall, what is Entity framework?<\/b><\/p>\n<p>Entity framework is a type of ORM (object relationship model) that connects classes with database objects. It makes it easier to work with databases and tables without worrying about columns and rows. ORM makes it easier for query databases using LINQ, and we do not need to worry about the database connection. Additionally, we can create programs that are unaware of the database and its connection or type (Oracle, SQL Server or MySQL). Entity framework takes care of all of the back-end connection so you can worry about the queries and data. Entity framework is a comprehensive tool when we want to work with databases in your .NET development platform.<\/p>\n<p><strong><a href=\"http:\/\/mairwa.com\/wordpress\/wp-content\/uploads\/2014\/08\/Untitled4.png\">\u00a0<\/a>30)Difference between ObjectContext and DbContext<\/strong><\/p>\n<p><b>ObjectContext<\/b><\/p>\n<p>ObjectContext is a class that manages all the database operations, like database connection, and manages various entities of the Entity Model. We can say that ObjectContext is the primary class for accessing or working together with entities that are defined in the conceptual model.<\/p>\n<p>&nbsp;<\/p>\n<p><b>DbContext<br \/>\n<\/b><br \/>\nDbContext is conceptually similar to ObjectContext. DbContext is nothing but a ObjectContext wrapper, we can say it is a lightweight alternative to the ObjectContext. DbContext can be used for DataBase first, code first and model first development. DbContext mainly contains a set of APIs that\u00a0are very easy to use. The API is exposed by ObjectContext. These APIs also allow us to use a Code First approach that ObjectContext does not allow.<\/p>\n<p>&nbsp;<\/p>\n<p><b>ObjectContext VS DbContext<\/b><\/p>\n<table width=\"80%\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"319\"><b>ObjectContext<\/b><\/td>\n<td valign=\"top\" width=\"487\"><b>DbContext<\/b><\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"319\">ObjectContext class is part of the core Entity Framework API, that allows us to perform queries, change and track updates of the Database by using strongly typed entity classes.<\/td>\n<td valign=\"top\" width=\"487\">The DbContext class can be described as a wrapper of ObjectContext. It exposes the most commonly used features of ObjectContext.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"319\">ObjectContext supports Compiled Queries.<\/td>\n<td valign=\"top\" width=\"487\">DbContext does not support Compiled Queries.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"319\">ObjectContext supports self-tracking of Entities<\/td>\n<td valign=\"top\" width=\"487\">DbContext does not support self-tracking of Entities.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"319\">ObjectContext is only useful in Model First and Database First approaches<\/td>\n<td valign=\"top\" width=\"487\">DbContext is useful in Model First, Database First approach as well as Code First approach.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"319\">ObjectContext can be used by Entity Framework 4.0 and below.<\/td>\n<td valign=\"top\" width=\"487\">DBContext can be used by Entity Framework 4.1 and above.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"319\">The ObjectContext class is not thread-safe.<\/td>\n<td valign=\"top\" width=\"487\">Any public static (C#) or Shared (Visual Basic) members of DbContext are thread-safe. Any instance members are not guaranteed to be thread safe.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>There are also many methods in common in both ObjectContext and DbContext. For example ObjectContext has a direct method to execute\u00a0a query against a database whereas the DbContext.DataBase class contains the SQLQuery method to\u00a0obtain the same result.<\/p>\n<p><b>Example<\/b><\/p>\n<p>\/\/ using ObjectContext (EF4.0)<br \/>\nusing\u00a0(Entities\u00a0context =\u00a0new\u00a0Entities())<br \/>\n{<br \/>\nIEnumerable&lt;EmployeeDetails&gt; empDetails \u00a0= \u00a0context.ExecuteStoreQuery&lt;EmployeeDetails&gt;<br \/>\n(&#8220;exec GetEmployeeData&#8221;).ToList();<br \/>\n}<\/p>\n<p>\/\/ using DBContext (EF 4.1 and above)<br \/>\nusing\u00a0(Entities\u00a0context =\u00a0new\u00a0Entities())<br \/>\n{<br \/>\nIEnumerable&lt;EmployeeDetails&gt; empDetails \u00a0= \u00a0context. Database.SqlQuery<br \/>\n&lt;\u00a0EmployeeDetails\u00a0&gt;(&#8220;exec\u00a0GetEmployeeData\u00a0&#8220;, null).ToList();<br \/>\n}<\/p>\n<p>DbContext also has some sets of methods that are not available with ObjectContext, like Dbset.Find, DbSet.Local etcetera.<\/p>\n<p>Some of the ObjectContext methods are also renamed. For example ObjectContext has methods like AddObject, DeleteObject And Attech on ObjectSet&lt;T&gt;, in DbContext, they are named Add, Remove and Attach on DbSet&lt;TEntity&gt;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\">1) What is the advantage and disadvantage of Entity Framework Advantages: One common syntax (LINQ) for all object queries whether it is database or not , Pretty fast if used as intended , easy to implement SoC , less coding required to accomplish complex tasks. Its fast and straight forward using LINQ\/FE objects For Add\/Modify\/Delete\/Update. Easy to map business objects&hellip; <a href=\"http:\/\/mairwa.com\/wordpress\/?p=95\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-95","post","type-post","status-publish","format-standard","hentry","category-ado-net-entity-framework","xfolkentry"],"_links":{"self":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/95","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=95"}],"version-history":[{"count":0,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/95\/revisions"}],"wp:attachment":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=95"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=95"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=95"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}