The last blog familiarized you with the Astoria Service. As promised this blog would take you through concepts like creating an EDM, creating ADO.NET Data Service, consuming an ADO.NET Data Service and also helping to understand LINQ to SQL and XML.
Before Starting there are some Software prerequisites to be born in mind
· Use VS 2008 Express Edition or VS 2008 RTM Version
· ASP.NET 3.5 Extensions Preview
· ADO.NET Entity Framework Setup
· ADO.NET Entity Framework Tools
· SQL Server 2005 with a table TblCategory(aCategoryId, tCategoryName)
Creating ADO.NET Entity Data Model
The first step to go ahead would be creating An Entity Data Model for the service to be created. To go ahead with it
1. Create an empty website in VS and right click on the project name and select ADO.NET Entity Data Model.
2. A wizard prompts to create the EDM for the Database you require and also creates the Entities and context objects for the EDM created.
3. At the end of it you would find the .edmx file (having the table associations and mappings diagrammatically) and an auto generated logic file with same name as given to the EDM. This contains all the mapping and associations. This also adds the configuration to the .config file.
Before Starting there are some Software prerequisites to be born in mind
· Use VS 2008 Express Edition or VS 2008 RTM Version
· ASP.NET 3.5 Extensions Preview
· ADO.NET Entity Framework Setup
· ADO.NET Entity Framework Tools
· SQL Server 2005 with a table TblCategory(aCategoryId, tCategoryName)
Creating ADO.NET Entity Data Model
The first step to go ahead would be creating An Entity Data Model for the service to be created. To go ahead with it
1. Create an empty website in VS and right click on the project name and select ADO.NET Entity Data Model.
2. A wizard prompts to create the EDM for the Database you require and also creates the Entities and context objects for the EDM created.
3. At the end of it you would find the .edmx file (having the table associations and mappings diagrammatically) and an auto generated logic file with same name as given to the EDM. This contains all the mapping and associations. This also adds the configuration to the .config file.
4. You can see the mapping by double clicking on the .edmx file in explorer.
Creating ADO.NET Data Service
We would now use the same EDM created above. ADO.NET Data Service which can be added by right clicking on the project name and selecting ADO.NET Data Service. Like WCF service it adds the logic file accompanied by the .svc file to the explorer.
Click on the WebDataService.cs ( according to the name given by you) and you see the something like below:
public class WebDataService : WebDataService
{
public static void InitializeService(IWebDataServiceConfiguration config)
{
// gives read permissions
config.SetResourceContainerAccessRule
("*", ResourceContainerRights.AllRead);
}
}
The Entity name( in this case have named it as TravelPortalEntities) forms one point of entry that associates this services to that particular model where in further querying as well as can use the stored procedures and views available in the EDM( provided you have selected it ).
Now you can view the .svc file you would be able to see the URL something like this
http://localhost:1028/adoservice/WebDataService.svc/
You can now query the above URL like
http://localhost:1028/adoservice/WebDataService.svc/tblCategory(1)
this gives the value from the table tblCategory with CategoryID 1
Consuming ADO.NET Data Service (Astoria)
We are now going to connect to the data service created above using Ajax.
Open an empty project and add DataService script class to it. This DataService class helps to execute data operations of a DataService we pointing do it. To create an instance of the DataService class
Var travelService= new Sys.Data.DataService(“TravelPortal.svc”);
TravelPoratl.svc is the name I gave to create the above service.
After you create the above instance you can all the query method to retrieve data from the data service.
Eg: travelService.query(“TblCategory”, cbSuccess, cbFailure);
This method references cbSuccess as the succeeded callback function and cbFailure as failed callback function, so accordingly you can frame both the references. You can also query with set to specific functions.
Hope this blogs helps you try on with Astoria.
public class WebDataService : WebDataService
public static void InitializeService(IWebDataServiceConfiguration config)
{
// gives read permissions
config.SetResourceContainerAccessRule
("*", ResourceContainerRights.AllRead);
}
}
The Entity name( in this case have named it as TravelPortalEntities) forms one point of entry that associates this services to that particular model where in further querying as well as can use the stored procedures and views available in the EDM( provided you have selected it ).
Now you can view the .svc file you would be able to see the URL something like this
http://localhost:1028/adoservice/WebDataService.svc/
You can now query the above URL like
http://localhost:1028/adoservice/WebDataService.svc/tblCategory(1)
this gives the value from the table tblCategory with CategoryID 1
Consuming ADO.NET Data Service (Astoria)
We are now going to connect to the data service created above using Ajax.
Open an empty project and add DataService script class to it. This DataService class helps to execute data operations of a DataService we pointing do it. To create an instance of the DataService class
Var travelService= new Sys.Data.DataService(“TravelPortal.svc”);
TravelPoratl.svc is the name I gave to create the above service.
After you create the above instance you can all the query method to retrieve data from the data service.
Eg: travelService.query(“TblCategory”, cbSuccess, cbFailure);
This method references cbSuccess as the succeeded callback function and cbFailure as failed callback function, so accordingly you can frame both the references. You can also query with set to specific functions.
Hope this blogs helps you try on with Astoria.