+91-90427 10472
         
Dot net training in Chennai -Maria Academy

Create wcf Application for CRUD operation

26 Oct 2016

Document by Ganesan – Ganesanva@hotmail.com – + 919600370429

-Create StudentDetails Table in Local DB

Right Click on the DB and Click Properties.

Connection String can be seen in Property window.

– Create a new WCF Service Application.
Put the connection string above in Web.config as in Snippet.

<connectionStrings>
<add name=”TestConnection” connectionString=”Data Source=(LocalDb)\v11.0;Initial Catalog=TestDB;Integrated Security=True;Pooling=False” providerName=”System.Data.SqlClient” />
</connectionStrings>


Create StudentDetails.cs Class file as below.

[DataContract]
public class StudentDetails
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string StudentName { get; set; }
[DataMember]
public int? Age { get; set; }
}

Create DBContext.Cs file as below

public class UsersContext : DbContext
{
public UsersContext()
: base(“TestConnection”)
{
}
public DbSet<StudentDetails> studentDbset { get; set; }
}

Open the default interface file IService1.cs

Insert the Snippet as below

[OperationContract]
List<StudentDetails> GetStudentData();
[OperationContract]
string CreateStudentData(StudentDetails objStudentDetails);
[OperationContract]
string UpdateStudentData(StudentDetails objStudentDetails);
[OperationContract]
StudentDetails GetStudentDatabyID(int id = 0);

Implement the below code in Service1.Svc.cs

Code Snippet

public List<StudentDetails> GetStudentData()
{
return db.studentDbset.ToList();
}
public string CreateStudentData(StudentDetails objStudentDetails)
{
db.studentDbset.Add(objStudentDetails);
db.SaveChanges();
return “Added”;
}
public string UpdateStudentData(StudentDetails objStudentDetails)
{
db.Entry(objStudentDetails).State = EntityState.Modified;
db.SaveChanges();
return “Updated”;
}
public StudentDetails GetStudentDatabyID(int id = 0)
{
StudentDetails studentdetails = db.studentDbset.Find(id);
return studentdetails;
}

Run the Service application as below

Click on the Link below to get the sample solution
https://1drv.ms/u/s!ArddhCoxftkQg6gdCJeDrlqBAJE0vg

Social tagging: > > > > > > > > > > > > > >