Database Reference
In-Depth Information
Our model represents blog posts and the comments that readers have about the posts. To make things clearer,
we've stripped out most of the properties that we would normally have, such as the body of the post, the author,
the date and time of the post, and so on.
We want to put all of the database code behind a WCF service so that clients can read, update, and delete posts
and comments, as well as insert new ones. To create the service, do the following:
1.
Create a new Visual Studio solution, adding a c# class library project. Name the class
library Recipe2 .
2.
Add a reference to the Entity Framework 6 libraries in the new project. Leveraging the
NuGet Package Manager does this best. Right-click on Reference, and select Manage
NuGet Packages. From the Online tab, locate and install the Entity Framework 6 package.
3.
Add three classes to the Recipe2 project: Post, Comment, and Recipe2Context. Post and
Comment represent POCO entity classes that will directly map to the corresponding
Post and Comment tables. Recipe2Context is the DbContext object that will serve as the
gateway to Entity Framework functionality. Make sure that you include the required WCF
DataContract and DataMember attributes in the entity classes as shown in Listing 9-7.
Listing 9-7. Our POCO Classes Post, Comment, and Our Recipe2Context Context Object
[DataContract(IsReference = true)]
public class Post
{
public Post()
{
comments = new HashSet<Comments>();
}
[DataMember]
public int PostId { get; set; }
[DataMember]
public string Title { get; set; }
[DataMember]
public virtual ICollection<Comment> Comments { get; set; }
}
[DataContract(IsReference=true)]
public class Comment
{
[DataMember]
public int CommentId { get; set; }
[DataMember]
public int PostId { get; set; }
[DataMember]
public string CommentText { get; set; }
[DataMember]
public virtual Post Post { get; set; }
}
public class EFRecipesEntities : DbContext
{
public EFRecipesEntities()
: base("name=EFRecipesEntities")
 
Search WWH ::




Custom Search