Use of DataMember in WCF

Posted by Syedshakeer under WCF on 11/26/2011 | Points: 10 | Views : 14522 | Status : [Member] | Replies : 1
What is the use of DataMember in WCF? Below is the code of Declaring DataMember.Where i Can use (or) call this datamember in WCF Programming.
Can you give a simple example of using DataMember...

public class MyDataContract

public string name = "myname";

[DataMember(IsRequired = true)]
public int age;


Syed Shakeer Hussain


Posted by: debal_saha-9451 on: 11/26/2011 [Member] Starter | Points: 25

DataMember attribute is required to expose the data along with contract of wcf services . If you don't use [ Datamember ] , then service can't able to transfer data(properties or fields). Use Using System.Runtime.Serialization to add datamember .
You can also use annotations like the below code .
[DataMember(Name = "NoteID", IsRequired = true)]
public int NoteID
{ get; set; }
Suppose, you are doing a CRUD application using SQL database . Using code you have to update database , at that time you should create a class which contains all the properties (respective to your database table's column name ) then use datamember attribute .
See the example :

Syedshakeer, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response