{"id":239,"date":"2014-08-27T06:41:41","date_gmt":"2014-08-27T06:41:41","guid":{"rendered":"http:\/\/mairwa.com\/wordpress\/?p=239"},"modified":"2014-08-27T06:41:41","modified_gmt":"2014-08-27T06:41:41","slug":"stored-proceduredbcontext-and-dbset-in-entity-framework","status":"publish","type":"post","link":"http:\/\/mairwa.com\/wordpress\/?p=239","title":{"rendered":"Stored procedure,Dbcontext and Dbset in Entity framework"},"content":{"rendered":"<p><code><b>Stored procedure use in EF<\/b><\/code><\/p>\n<p>Stored procedures and user-defined functions (UDFs) in the database are represented as functions in entity framework. So EDM won\u2019t have any entity or other stuff for stored procedures in the EDM designer.<\/p>\n<p>USE [MyDB]<\/p>\n<p>GO<\/p>\n<p>\/****** Object:\u00a0 StoredProcedure [dbo].[InsertRole]\u00a0\u00a0\u00a0 Script Date: 08\/27\/2014 11:10:29 ******\/<\/p>\n<p>SET ANSI_NULLS ON<\/p>\n<p>GO<\/p>\n<p>SET QUOTED_IDENTIFIER ON<\/p>\n<p>GO<\/p>\n<p>CREATE PROCEDURE [dbo].[InsertRole]<\/p>\n<p>(<\/p>\n<p>@roleName varchar(100)<\/p>\n<p>)<\/p>\n<p>AS<\/p>\n<p>BEGIN<\/p>\n<p>INSERT INTO [Role]<\/p>\n<p>([RoleName])<\/p>\n<p>VALUES<\/p>\n<p>(@roleName)<\/p>\n<p>END<\/p>\n<p>We \u00a0would see in InsertRole stored procedure added in <b>Function Imports<\/b> with new complex type InsertRole_Result in Model Browser. Whenever we import stored procedure into model, it creates new complex type with the name <i>{sp name}<\/i>_Result by default.<\/p>\n<p>Following example is that how to define insertrole sp in model namespace and entities class.<\/p>\n<p>Function imports are based on stored procedures. To map a function import to a complex type, the columns returned by the corresponding stored procedure must match the properties of the complex type in number and must have storage types that are compatible with the property types.<\/p>\n<p><b>DbContext &#8212;<\/b>A DbContext instance represents a combination of the Unit Of Work and Repository patterns such that it can be used to query from a database and group together changes that will then be written back to the store as a unit.\u00a0 DbContext is conceptually similar to ObjectContext.<\/p>\n<p>&nbsp;<\/p>\n<p>When using the Code First approach, the System.Data.Entity.DbSet&lt;TEntity&gt;properties on the derived context are used to build a model by convention.<\/p>\n<p>&nbsp;<\/p>\n<p>namespace TypeSettingWeb.Models<\/p>\n<p>{<\/p>\n<p>using System;<\/p>\n<p>using System.Data.Entity;<\/p>\n<p>using System.Data.Entity.Infrastructure;<\/p>\n<p>using System.Data.Entity.Core.Objects;<\/p>\n<p>using System.Linq;<\/p>\n<p>public partial class MyEntities : DbContext<\/p>\n<p>{<\/p>\n<p>public MyEntities()<\/p>\n<p>: base(&#8220;name=MyEntities&#8221;)<\/p>\n<p>{<\/p>\n<p>}<\/p>\n<p>public virtual int InsertRole(string roleName)<\/p>\n<p>{<\/p>\n<p>var roleNameParameter = roleName != null ?<\/p>\n<p>new ObjectParameter(&#8220;roleName&#8221;, roleName) :<\/p>\n<p>new ObjectParameter(&#8220;roleName&#8221;, typeof(string));<\/p>\n<p>return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction(&#8220;InsertRole&#8221;, roleNameParameter);<\/p>\n<p>}<\/p>\n<p>}}<\/p>\n<p>&nbsp;<\/p>\n<p><b>DbSet &#8212;<\/b>A DbSet represents the collection of all entities in the context, or that can be queried from the database, of a given type. DbSet objects are created from a DbContext using the DbContext.Set method.<\/p>\n<p>Following is use for Datatable mapping to EF.it is using of Dbset properties.<\/p>\n<p>public virtual DbSet&lt;ActionPermission&gt; ActionPermissions { get; set; }<\/p>\n<p><b><i>\u00a0<\/i><\/b><\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\">Stored procedure use in EF Stored procedures and user-defined functions (UDFs) in the database are represented as functions in entity framework. So EDM won\u2019t have any entity or other stuff for stored procedures in the EDM designer. USE [MyDB] GO \/****** Object:\u00a0 StoredProcedure [dbo].[InsertRole]\u00a0\u00a0\u00a0 Script Date: 08\/27\/2014 11:10:29 ******\/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE PROCEDURE [dbo].[InsertRole]&hellip; <a href=\"http:\/\/mairwa.com\/wordpress\/?p=239\">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":[3],"tags":[],"class_list":["post-239","post","type-post","status-publish","format-standard","hentry","category-c-vb","xfolkentry"],"_links":{"self":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/239","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=239"}],"version-history":[{"count":0,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/239\/revisions"}],"wp:attachment":[{"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=239"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=239"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/mairwa.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}