How to: Create a Data Service Using an ADO.NET Entity Framework Data Source (WCF Data Services)
WCF Data Services exposes entity data as a data service. This entity data is provided by the ADO.NETEntity Framework when the data source is a relational database. This topic shows you how to create an Entity Framework-based data model in a Visual Studio Web application that is based on an existing database and use this data model to create a new data service.
The Entity Framework also provides a command line tool that can generate an Entity Framework model outside of a Visual Studio project. For more information, see How to: Use EdmGen.exe to Generate the Model and Mapping Files.
To add an Entity Framework model that is based on an existing database to an existing Web application
On the Project menu, click Add > New Item.
In the Templates pane, click the Data category, and then select ADO.NET Entity Data Model.
Enter the model name and then click Add.
The first page of the Entity Data Model Wizard is displayed.
In the Choose Model Contents dialog box, select Generate from database. Then click Next.
Click the New Connection button.
In the Connection Properties dialog box, type your server name, select the authentication method, type the database name, and then click OK.
The Choose Your Data Connection dialog box is updated with your database connection settings.
Ensure that the Save entity connection settings in App.Config as: checkbox is checked. Then click Next.
In the Choose Your Database Objects dialog box, select all of database objects that you plan to expose in the data service.
Objects included in the data model are not automatically exposed by the data service. They must be explicitly exposed by the service itself. For more information, see Configuring the Data Service.
Click Finish to complete the wizard.
This creates a default data model based on the specific database. The Entity Framework enables to customize the data model. For more information, see Entity Data Model Tools Tasks.
To create the data service by using the new data model
In Visual Studio, open the .edmx file that represents the data model.
In the Model Browser, right-click the model, click Properties, and then note the name of the entity container.
In Solution Explorer, right-click the name of your ASP.NET project, and then click Add > New Item.
In the Add New Item dialog box, select the WCF Data Service template in the Web category.
The WCF Data Service template is available in Visual Studio 2015, but not in Visual Studio 2017.
Supply a name for the service, and then click OK.
Visual Studio creates the XML markup and code files for the new service. By default, the code-editor window opens.
In the code for the data service, replace the comment
/* TODO: put your data source class name here */in the definition of the class that defines the data service with the type that inherits from the ObjectContext class and that is the entity container of the data model, which was noted in step 2.
In the code for the data service, enable authorized clients to access the entity sets that the data service exposes. For more information, see Creating the Data Service.
To test the Northwind.svc data service by using a Web browser, follow the instructions in the topic Accessing the Service from a Web Browser.
We'd love to hear your thoughts. Choose the type you'd like to provide:
Our feedback system is built on GitHub Issues. Read more on our blog.