{"id":804,"date":"2014-01-14T14:25:29","date_gmt":"2014-01-14T14:25:29","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/webdev\/2014\/01\/14\/scaffolding-ado-net-entity-data-model-designer-based-models\/"},"modified":"2022-08-09T02:52:49","modified_gmt":"2022-08-09T09:52:49","slug":"scaffolding-ado-net-entity-data-model-designer-based-models","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/scaffolding-ado-net-entity-data-model-designer-based-models\/","title":{"rendered":"Scaffolding ADO.NET Entity Data Model Designer-based-models"},"content":{"rendered":"<p>The ADO.NET Entity Data Model Designer (Entity Designer) is a GUI tool for editing Entity Framework models. You can use the Entity Designer to visually create and modify entities, associations, mappings, and inheritance relationships. You can also use the tool to validate the model. <\/p>\n<p>When you build the Entity Framework models, Entity Designer in Visual Studio 2013 generates a <b>DbContext<\/b> based container with the associated T4 template. These context files do not carry any metadata information for the target EDMX files. They are generated to always throw the <b>UnintentionalCodeFirstException<\/b> as follows:<\/p>\n<div id=\"scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:ae6a4dc4-98c6-4a9b-9ca9-a31209853753\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px;padding: 0px;float: none\">\n<div style=\"border: #000080 1px solid;color: #000;font-family: 'Courier New', Courier, Monospace;font-size: 10pt\">\n<div style=\"background: #000080;color: #fff;font-family: Verdana, Tahoma, Arial, sans-serif;font-weight: bold;padding: 2px 5px\">Code Snippet<\/div>\n<div style=\"background: #ddd;max-height: 300px;overflow: auto\">\n<ol start=\"1\" style=\"background: #ffffff;margin: 0 0 0 2em;padding: 0 0 0 5px\">\n<li><span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">protected<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#0000ff\">override<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#0000ff\">void<\/span><span style=\"background:#ffffff;color:#000000\"> OnModelCreating(DbModelBuilder modelBuilder)<\/span><\/li>\n<li style=\"background: #f3f3f3\"><span style=\"background:#ffffff;color:#000000\">{<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">throw<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#0000ff\">new<\/span><span style=\"background:#ffffff;color:#000000\"> UnintentionalCodeFirstException();<\/span><\/li>\n<li style=\"background: #f3f3f3\"><span style=\"background:#ffffff;color:#000000\">}<\/span><\/li>\n<\/ol><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<p>Note:    <br \/>See <a href=\"http:\/\/blog.oneunicorn.com\/2012\/02\/26\/dont-use-code-first-by-mistake\/\">http:\/\/blog.oneunicorn.com\/2012\/02\/26\/dont-use-code-first-by-mistake\/<\/a>, for more information about <b>UnintentionalCodeFirstException<\/b>.<\/p>\n<p>Instead, the metadata information that you described in Entity Designer is embedded into the target assemblies as resources when the <b>Metadata Artifact Processing<\/b> property of the EDMX file is set to <b>Embed in Output Assembly<\/b>, which is default. If the property is set to <b>Copy to Output Directory<\/b>, it is copied to separate files. For both cases, Entity Designer adds an <b>EntityConnection <\/b>connection string into the Web.config file of the target Web project as follows:<\/p>\n<div id=\"scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:4216242c-c2ed-4079-a04d-00ab74b5635c\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px;padding: 0px;float: none\">\n<div style=\"border: #000080 1px solid;color: #000;font-family: 'Courier New', Courier, Monospace;font-size: 10pt\">\n<div style=\"background: #000080;color: #fff;font-family: Verdana, Tahoma, Arial, sans-serif;font-weight: bold;padding: 2px 5px\">Code Snippet<\/div>\n<div style=\"background: #ddd;max-height: 300px;overflow: auto\">\n<ol start=\"1\" style=\"background: #ffffff;margin: 0 0 0 2.5em;padding: 0 0 0 5px\">\n<li><span style=\"background:#ffffff;color:#0000ff\">&lt;<\/span><span style=\"background:#ffffff;color:#a31515\">configuration<\/span><span style=\"background:#ffffff;color:#0000ff\">&gt;<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0<span style=\"background:#ffffff;color:#0000ff\">&lt;<\/span><span style=\"background:#ffffff;color:#a31515\">connectionStrings<\/span><span style=\"background:#ffffff;color:#0000ff\">&gt;<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#0000ff\">&lt;<\/span><span style=\"background:#ffffff;color:#a31515\">add<\/span><span style=\"background:#ffffff;color:#0000ff\"> <\/span><span style=\"background:#ffffff;color:#ff0000\">name<\/span><span style=\"background:#ffffff;color:#0000ff\">=<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\">Northwind_Entities<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#0000ff\"><\/span><span style=\"background:#ffffff;color:#ff0000\">connectionString<\/span><span style=\"background:#ffffff;color:#0000ff\">=<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\">metadata=res:\/\/*\/Northwind.csdl|<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#0000ff\">res:\/\/*\/Northwind.ssdl|<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#0000ff\">res:\/\/*\/Northwind.msl;<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#0000ff\">provider=System.Data.SqlClient;<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#0000ff\">provider connection string=<\/span><span style=\"background:#ffffff;color:#ff0000\">&amp;quot;<\/span><span style=\"background:#ffffff;color:#0000ff\">Data Source=.&#092;sqlexpress;nitial Catalog=Northwind;Integrated Security=True;MultipleActiveResultSets=True<\/span><span style=\"background:#ffffff;color:#ff0000\">&amp;quot;<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#0000ff\"><\/span><span style=\"background:#ffffff;color:#ff0000\">providerName<\/span><span style=\"background:#ffffff;color:#0000ff\">=<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\">System.Data.EntityClient<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\">\/&gt;<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0<span style=\"background:#ffffff;color:#0000ff\">&lt;\/<\/span><span style=\"background:#ffffff;color:#a31515\">connectionStrings<\/span><span style=\"background:#ffffff;color:#0000ff\">&gt;<\/span><\/li>\n<li><span style=\"background:#ffffff;color:#0000ff\">&lt;\/<\/span><span style=\"background:#ffffff;color:#a31515\">configuration<\/span><span style=\"background:#ffffff;color:#0000ff\">&gt;<\/span><\/li>\n<\/ol><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<p>Entity Designer also creates a <b>DbContext<\/b> constructor to have <b>DbContext<\/b> use the connection string to retrieve the metadata information by passing the connection string name. For example:<\/p>\n<div id=\"scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:04001f47-b317-4952-84f4-7d2463f736e3\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px;padding: 0px;float: none\">\n<div style=\"border: #000080 1px solid;color: #000;font-family: 'Courier New', Courier, Monospace;font-size: 10pt\">\n<div style=\"background: #000080;color: #fff;font-family: Verdana, Tahoma, Arial, sans-serif;font-weight: bold;padding: 2px 5px\">Code Snippet<\/div>\n<div style=\"background: #ddd;max-height: 300px;overflow: auto\">\n<ol start=\"1\" style=\"background: #ffffff;margin: 0 0 0 2em;padding: 0 0 0 5px\">\n<li><span style=\"background:#ffffff;color:#0000ff\">public<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#0000ff\">class<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#2b91af\">NorthwindContext<\/span><span style=\"background:#ffffff;color:#000000\"> : DbContext<\/span><\/li>\n<li style=\"background: #f3f3f3\"><span style=\"background:#ffffff;color:#000000\">{<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">public<\/span><span style=\"background:#ffffff;color:#000000\"> NorthwindContext()<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">: <\/span><span style=\"background:#ffffff;color:#0000ff\">base<\/span><span style=\"background:#ffffff;color:#000000\">(<\/span><span style=\"background:#ffffff;color:#a31515\">&quot;name=Northwind_Entities&quot;<\/span><span style=\"background:#ffffff;color:#000000\">)<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">{<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">}<\/span><\/li>\n<li><span style=\"background:#ffffff;color:#000000\">}<\/span><\/li>\n<\/ol><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<p>Note: to see more information about how Entity Framework uses connection strings, see <a href=\"http:\/\/msdn.microsoft.com\/en-US\/data\/jj592674\">http:\/\/msdn.microsoft.com\/en-US\/data\/jj592674<\/a>.<\/p>\n<p>To scaffold the models with the connection strings, you have to choose the generated container for the data context classes. In this walkthrough, you will scaffold the EDMX generated models for Model First approach. You should be able to use the same tool for Database First approach in a similar way. <\/p>\n<p>1. Open an existing MVC 5 project in Visual Studio 2013 or create a new MVC 5 project. In this walkthrough, we will use the MVC project template with the default options. <\/p>\n<p>2. Create a sample model with Entity Designer<\/p>\n<blockquote>\n<p>a. In Solution Explorer, under the <b>Models<\/b> folder, add a new<b> ADO.NET Entity Data Model <\/b>item called <b>BloggingModel<\/b>. <\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/aspnet\/wp-content\/uploads\/sites\/16\/2014\/01\/0564.image_thumb_691E186F.png\"><img decoding=\"async\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/aspnet\/wp-content\/uploads\/sites\/16\/2014\/01\/0564.image_thumb_691E186F.png\" width=\"399\" height=\"277\" \/><\/a> <\/p>\n<\/blockquote>\n<blockquote>\n<p>b. In the following Entity Data Model Wizard, select <b>Empty model<\/b>, and then click <b>Finish<\/b>.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/aspnet\/wp-content\/uploads\/sites\/16\/2014\/01\/1157.image_thumb_02861BAA.png\"><img decoding=\"async\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/aspnet\/wp-content\/uploads\/sites\/16\/2014\/01\/1157.image_thumb_02861BAA.png\" width=\"421\" height=\"378\" \/><\/a> <\/p>\n<\/p>\n<\/blockquote>\n<p>3. The Entity Designer is opened with a blank model. Now we can start adding entities, properties and associations to the model.<\/p>\n<blockquote>\n<p>a. Right-click on the design surface and select <b>Properties.<\/b><\/p>\n<\/blockquote>\n<blockquote>\n<p>b. In the <b>Properties<\/b> window change the <b>Entity Container Name<\/b> to <b>BloggingContext.<\/b>       <br \/>This is the name of the derived context that will be generated for you, the context represents a session with the database, allowing us to query and save data.<\/p>\n<\/blockquote>\n<blockquote>\n<p>c. Right-click on the design surface and select <b>Add New -&gt; Entity<\/b>. <\/p>\n<\/blockquote>\n<blockquote>\n<p>d. Enter <b>Blog<\/b> as the entity name and <b>BlogId<\/b> as the key name and click <b>OK.<\/b><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/aspnet\/wp-content\/uploads\/sites\/16\/2014\/01\/0160.image_thumb_2DCAB2B1.png\"><img decoding=\"async\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/aspnet\/wp-content\/uploads\/sites\/16\/2014\/01\/0160.image_thumb_2DCAB2B1.png\" width=\"440\" height=\"529\" \/><\/a> <\/p>\n<\/blockquote>\n<blockquote>\n<p>e. Right-click on the new entity on the design surface and select <b>Add New -&gt; Scalar Property<\/b>, enter <b>Name<\/b> as the name of the property.<\/p>\n<p>f. Repeat this process to add a <b>Url<\/b> property.<\/p>\n<p>g. Right-click on the <b>Url<\/b> property on the design surface and select <b>Properties<\/b>, in the <b>Properties<\/b> window change the <b>Nullable<\/b> setting to True       <br \/>This allows us to save a Blog to the database without assigning it a <b>Url.<\/b><\/p>\n<p>h. Using the same steps used to create the <b>Blog<\/b> entity, add a <b>Post<\/b> entity with a <b>PostId<\/b> key property.<\/p>\n<p>i. Add <b>Title<\/b> and <b>Content<\/b> scalar properties to the Post entity.<\/p>\n<\/blockquote>\n<p>4. Now that we have a couple of entities, add an association (or relationship) between them.<\/p>\n<blockquote>\n<p>a. Right-click on the design surface and select <b>Add New -&gt; Association<\/b>.<\/p>\n<p>b. Make one end of the relationship point to <b>Blog<\/b> with a multiplicity of <b>One<\/b> and the other end point to <b>Post<\/b> with a multiplicity of <b>Many.<\/b>       <br \/>This means that a Blog has many Posts and a Post belongs to one Blog.<\/p>\n<p>c. Ensure the <b>Add foreign key properties to &#8216;Post&#8217; Entity<\/b> box is checked and click <b>OK<\/b>.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/aspnet\/wp-content\/uploads\/sites\/16\/2014\/01\/8507.image_thumb_590F49B8.png\"><img decoding=\"async\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/aspnet\/wp-content\/uploads\/sites\/16\/2014\/01\/8507.image_thumb_590F49B8.png\" width=\"497\" height=\"551\" \/><\/a> <\/p>\n<\/blockquote>\n<p>5. Given our model, Entity Framework can calculate a database schema that will allow us to store and retrieve data using the model. To do that, we should map the model to the physical database. For Model First Approach, we will generate a database from the model. <\/p>\n<p>Note:    <br \/>You do not need to perform this step in Database First Approach because your models are already mapped to the database. For more information about how to use Entity Designer for Database First approach, see <a href=\"http:\/\/msdn.microsoft.com\/en-us\/data\/jj206878.aspx\">http:\/\/msdn.microsoft.com\/en-us\/data\/jj206878.aspx<\/a>.<\/p>\n<blockquote>\n<p>a. Right-click on the design surface and select <b>Generate Database from Model<\/b>. <\/p>\n<p>b. Click <b>New Connection<\/b>, select <b>Microsoft SQL Server<\/b> for <b>Data source<\/b>, specify <b>(localdb)v11.0<\/b>, and then enter <b>ModelFirst.Blogging<\/b> as the database name.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/aspnet\/wp-content\/uploads\/sites\/16\/2014\/01\/2727.image_thumb_4489FA3A.png\"><img decoding=\"async\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/aspnet\/wp-content\/uploads\/sites\/16\/2014\/01\/2727.image_thumb_4489FA3A.png\" width=\"404\" height=\"588\" \/><\/a> <\/p>\n<p>c. Select <b>OK<\/b> and you will be asked if you want to create a new database, select <b>Yes. <\/b>Make sure that the <b>Save entity connection settings in Web.Config<\/b> check box is selected.<\/p>\n<p>d. Select <b>Next<\/b> and the Entity Framework Designer will calculate a script to create the database schema.<\/p>\n<p>e. Once the script is displayed, click <b>Finish<\/b> and the script will be added to your project and opened<\/p>\n<p>f. Right-click on the script and select <b>Execute<\/b>, you will be prompted to specify the database to connect to, specify <b>(localdb)v11.0<\/b>.<\/p>\n<\/blockquote>\n<p>6. Build the project. The Entity Designer generates two entity classes, Blog and Post, and the database context file as follows:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/aspnet\/wp-content\/uploads\/sites\/16\/2014\/01\/5282.image_thumb_6BC4436F.png\"><img decoding=\"async\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/aspnet\/wp-content\/uploads\/sites\/16\/2014\/01\/5282.image_thumb_6BC4436F.png\" width=\"355\" height=\"424\" \/><\/a> <\/p>\n<blockquote>\n<p>a. Open the <i>Web.config<\/i> file to verify the connection string that Entity Designer adds. The name of the generated connection string should have the same name that you set for the <b>Entity Container Name<\/b> property. In this walkthrough, it is <b>BloggingContext<\/b>. <\/p>\n<div id=\"scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:1347e1b3-8c26-4c3d-8e83-b2784a07e3e3\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px;padding: 0px;float: none\">\n<div style=\"border: #000080 1px solid;color: #000;font-family: 'Courier New', Courier, Monospace;font-size: 10pt\">\n<div style=\"background: #000080;color: #fff;font-family: Verdana, Tahoma, Arial, sans-serif;font-weight: bold;padding: 2px 5px\">Code Snippet<\/div>\n<div style=\"background: #ddd;max-height: 300px;overflow: auto\">\n<ol start=\"1\" style=\"background: #ffffff;margin: 0 0 0 2em;padding: 0 0 0 5px\">\n<li><span style=\"background:#ffffff;color:#0000ff\">&lt;<\/span><span style=\"background:#ffffff;color:#a31515\">add<\/span><span style=\"background:#ffffff;color:#0000ff\"> <\/span><span style=\"background:#ffffff;color:#ff0000\">name<\/span><span style=\"background:#ffffff;color:#0000ff\">=<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\">BloggingContext<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\"> <\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#0000ff\"><\/span><span style=\"background:#ffffff;color:#ff0000\">connectionString<\/span><span style=\"background:#ffffff;color:#0000ff\">=<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\">metadata=res:\/\/*\/BloggingModel.csdl|res:\/\/*\/BloggingModel.ssdl|res:\/\/*\/BloggingModel.msl;provider=System.Data.SqlClient;provider connection string=<\/span><span style=\"background:#ffffff;color:#ff0000\">&amp;quot;<\/span><span style=\"background:#ffffff;color:#0000ff\">data source=(localdb)&#092;v11.0;initial catalog=ModelFirst.Blogging;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework<\/span><span style=\"background:#ffffff;color:#ff0000\">&amp;quot;<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\"> <\/span><\/li>\n<li><span style=\"background:#ffffff;color:#ff0000\">providerName<\/span><span style=\"background:#ffffff;color:#0000ff\">=<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\">System.Data.EntityClient<\/span><span style=\"background:#ffffff;color:#000000\">&quot;<\/span><span style=\"background:#ffffff;color:#0000ff\"> \/&gt;<\/span><\/li>\n<\/ol><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/blockquote>\n<blockquote>\n<p>b. Open <b>BloggingModel.Context.cs<\/b> to verify the constructor generated to use the connection string. <\/p>\n<div id=\"scid:9ce6104f-a9aa-4a17-a79f-3a39532ebf7c:937798a2-6474-499e-9358-d6db13a04b60\" class=\"wlWriterEditableSmartContent\" style=\"margin: 0px;padding: 0px;float: none\">\n<div style=\"border: #000080 1px solid;color: #000;font-family: 'Courier New', Courier, Monospace;font-size: 10pt\">\n<div style=\"background: #000080;color: #fff;font-family: Verdana, Tahoma, Arial, sans-serif;font-weight: bold;padding: 2px 5px\">Code Snippet<\/div>\n<div style=\"background: #ddd;max-height: 300px;overflow: auto\">\n<ol start=\"1\" style=\"background: #ffffff;margin: 0 0 0 2.5em;padding: 0 0 0 5px\">\n<li><span style=\"background:#ffffff;color:#0000ff\">public<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#0000ff\">partial<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#0000ff\">class<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#2b91af\">BloggingContext<\/span><span style=\"background:#ffffff;color:#000000\"> : DbContext<\/span><\/li>\n<li style=\"background: #f3f3f3\"><span style=\"background:#ffffff;color:#000000\">{<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">public<\/span><span style=\"background:#ffffff;color:#000000\"> BloggingContext()<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">: <\/span><span style=\"background:#ffffff;color:#0000ff\">base<\/span><span style=\"background:#ffffff;color:#000000\">(<\/span><span style=\"background:#ffffff;color:#a31515\">&quot;name=BloggingContext&quot;<\/span><span style=\"background:#ffffff;color:#000000\">)<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">{<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">}<\/span><\/li>\n<li>&nbsp;<\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">protected<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#0000ff\">override<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#0000ff\">void<\/span><span style=\"background:#ffffff;color:#000000\"> OnModelCreating(DbModelBuilder modelBuilder)<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">{<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">throw<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#0000ff\">new<\/span><span style=\"background:#ffffff;color:#000000\"> UnintentionalCodeFirstException();<\/span><\/li>\n<li>\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\">}<\/span><\/li>\n<li style=\"background: #f3f3f3\">&nbsp;<\/li>\n<li>\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">public<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#0000ff\">virtual<\/span><span style=\"background:#ffffff;color:#000000\"> DbSet&lt;Blog&gt; Blogs { <\/span><span style=\"background:#ffffff;color:#0000ff\">get<\/span><span style=\"background:#ffffff;color:#000000\">; <\/span><span style=\"background:#ffffff;color:#0000ff\">set<\/span><span style=\"background:#ffffff;color:#000000\">; }<\/span><\/li>\n<li style=\"background: #f3f3f3\">\u00a0\u00a0\u00a0\u00a0<span style=\"background:#ffffff;color:#000000\"><\/span><span style=\"background:#ffffff;color:#0000ff\">public<\/span><span style=\"background:#ffffff;color:#000000\"> <\/span><span style=\"background:#ffffff;color:#0000ff\">virtual<\/span><span style=\"background:#ffffff;color:#000000\"> DbSet&lt;Post&gt; Posts { <\/span><span style=\"background:#ffffff;color:#0000ff\">get<\/span><span style=\"background:#ffffff;color:#000000\">; <\/span><span style=\"background:#ffffff;color:#0000ff\">set<\/span><span style=\"background:#ffffff;color:#000000\">; }<\/span><\/li>\n<li><span style=\"background:#ffffff;color:#000000\">}<\/span><\/li>\n<\/ol><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/blockquote>\n<p>7. Build your project again to make sure everything works fine. You can now use new ASP.NET Scaffolding as usual for the EDMX generated models, Blog and Post. Just do not forget to select the newly generated <b>BloggingContext<\/b> for <b>Data context class<\/b> while scaffolding as follows: <\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/aspnet\/wp-content\/uploads\/sites\/16\/2014\/01\/4214.image_thumb_7E0D0A31.png\"><img decoding=\"async\" title=\"image\" border=\"0\" alt=\"image\" src=\"https:\/\/devblogs.microsoft.com\/aspnet\/wp-content\/uploads\/sites\/16\/2014\/01\/4214.image_thumb_7E0D0A31.png\" width=\"604\" height=\"429\" \/><\/a> <\/p>\n<p>Note:    <br \/>For more information on how to use new MVC5 ASP.NET Scaffolding, see <a href=\"http:\/\/www.asp.net\/visual-studio\/overview\/2013\/aspnet-scaffolding-overview\">http:\/\/www.asp.net\/visual-studio\/overview\/2013\/aspnet-scaffolding-overview<\/a>. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>The ADO.NET Entity Data Model Designer (Entity Designer) is a GUI tool for editing Entity Framework models. You can use the Entity Designer to visually create and modify entities, associations, mappings, and inheritance relationships. You can also use the tool to validate the model. When you build the Entity Framework models, Entity Designer in Visual [&hellip;]<\/p>\n","protected":false},"author":428,"featured_media":58792,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[197],"tags":[7333,34,7436,7374,7389],"class_list":["post-804","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-aspnet","tag-asp-net-mvc","tag-asp-net-web-api","tag-visual-studio-2013","tag-web-deployment-tool","tag-web-development"],"acf":[],"blog_post_summary":"<p>The ADO.NET Entity Data Model Designer (Entity Designer) is a GUI tool for editing Entity Framework models. You can use the Entity Designer to visually create and modify entities, associations, mappings, and inheritance relationships. You can also use the tool to validate the model. When you build the Entity Framework models, Entity Designer in Visual [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/804","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/users\/428"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/comments?post=804"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/804\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/58792"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=804"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=804"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=804"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}