In this article you will learn how to create ASP.NET MVC APP with WCF Service using ADO.NET Entity Data Model.
Use the following to create an ASP.NET MVC APP with a WCF Service using an ADO.NET Entity Data Model:SQL Server DB -> WCF Service (ORM) -> MVC Application -> User (Browser).This small app works in a 4-tier architecture as in the following:
Project 1: WCF ProjectStep 1 Go to VS 2012 and select "File" -> "New Website" then select "WCF Service" then provide the name “WcfMvc”.Click "OK".Step 2Go to the Solution Explorer then go to "WcfMvc application" then right-click on it then seelct "Add" -> "Add new item" -> "Add ADO .Net Entity data model". Then click the "Add" button then place the file in the App_code Folder.Step 3Then the Entity Data Model Wizard will be shown. Select "Generate data from database" then click the "Next" button .Step 4Choose your data connection. I’m selecting My “ran” database of SqlServer 2012.Activate the radio button “Yes include the sensitive data in the connection string”.Save the connection string in the config file by enabling the check box “Save entity connection setting in web.config as:“ then click the "Next" button.Orgo to the new connection and create your connection then provide the server name then provide the authentication type then select either Windows Authentication or SQL Authentication then provide the database name then click the "Ok" button. A new connection will then be generated.Activate the Radio Button “Yes include the sensitive data in the connection string”.Save the connection string in the config file by enabling the check box “save entity connection setting in web.config as: “ then click the "Next" button.Step 5Choose which database object you want in your model.I’m selecting the “Customer” table. The table definition is as follows:Create table customer Custno int constraint pk primary key,custname varchar(30) not null,custcity varchar(30),custbalance money);You can use the above table or create your own table and use itThen provide a Model Namespace of your choice.. I’m using “ranjeet” Namespace.Click the "Finish" button.Please ensure that in the Solution Explorer under the App_code folder the Model.edmx file has been created.Your entity data model is ready for use.Step 6Go to Solution Explorer then expand the App_code Folder then go to the Iservice.cs file.Step 7Delete the Getdata() and GetDataUsingDataContract() methods.ThenI’m writing one method GetCustomer() as follows.Step 8Then go to the Sevice.cs file and implement the method declared in the IService.cs interface.In the Service.cs file right-click the Iservice Interface then select "Implement Interface" -> "Implement Interface".Delete the already present method in the Service.cs the file.And write the following code in the “public List<customer> GetCustomer()” method.“ranEntities1” is a class name given when the connection is created.Step 9Open the Service.svc file and now run the project.A new window will open; copy the URL present in that window and paste it into any text file. This URL will be used in the next project.Project 2: MVC Application 4Step 1Ensure that the WCF project is open then start another instance of VS 2012. Then go to "File" -> "New" -> "Project..." then create an ASP.NET MVC 4 Web Application. Name it “MvcWcfApplication” then click "ok" Button. Then select the Project Template “Internet Application” then click the "Ok" button.Step 2Create a Proxy using “Add Service Reference” by right-clicking on MvcWcfApplication in the Solution Explorer.(Note: Proxy is a class that converts a local request into a remote request.)A new window will open. Paste the URL that you copied from the WcfMvc project into the Address TextBox then click "Go".In other words, a proxy class is created successfully..(Caution: if it has been created and in the project you can’t use the ServiceReference1 namespace Pl then don’t worry; it is a VS 2012 Automation Error. To solve it go to the Solution Explorer then select ServiceReference1 then right-click on it then select "Configure Service Reference". A new window will open then in it uncheck the “Reuse types in referenced assemblies” checkbox..)A serviceReference1 will added into your project.Step 3Now add a new Controller then in Solution Explorer seelct "Controller" then right-click on it then select "Add Controller" (Ctrl+m, Ctrl+c).Name it DbController. Click the "Add" button.Step 4Add the following code in the DbController.cs file.Please build the project.Step 5Now right-click inside the Index() Method then select "Add View" (Ctrl+m, Ctrl+v) then click on “Create a Strongly Typed View” checkbox then select the Model Class we created, in other words “customer (MvcWcfApplication.ServiceReference1)” then select "Scaffold template List"then click the "Add" button.Step 6Run the application.In the browser type: http://localhost:<Port>/DbAnd see the results. All the data in the customer table is shown below the using WCF Service.If you like this then please comment below.THANK YOU.
Hands on ASP.NET GridView