create a wcf service for to insert and update data using entity freamework in wcf
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Ramesh MaruthiPosted Sep 3, 2014, 11:36 AM
please follow these code for inserting and updating you can do at a time for an example write an operation in your service interface file as UpsertStudent in this way, if it success it gives you the value as true if not false
namespace WCFandEFService
{
[ServiceContract]
public partial interface IPrasad
{
[OperationContract]
bool UpsertStudent( Student student);
}
[DataContract]
public class Student
{
[DataMember]
public int StudentID { get; set; }
[DataMember]
public string StudentName { get; set; }
[DataMember]
public string StudentField{ get; set; }
[DataMember]
public string StuentGrades { get; set; }
}
}
Than next in your .svc file
public partial class PrasadService: IIPrasad
{
public bool UpsertStudent( Student student)
{
Entities dbcontext = new Entities ();
Student existingstu = dbcontext.Students.Where( dl =>dl.StudentID== student.StudentID).FirstOrDefault();
// if the existing students count not equal to null you will update their values with new values
if (existingstu != null)
{
existingstu.StudentName = student.StudentName;
existingstu.StudentField = student.StudentField;
existingstu.StudentGrades = student.StudentGrades;
}
// else if existing is null you will insert the new student into database
else
{
Student s = new Student
{
StudentID = student.StudentID ,
StudentName = student.StudentName ,
StudentField= student.StudentField,
StudentGrades = student.StudentGrades
};
dbcontext.Students.Add(s)
}
// try to make changes to the database if everything goes well it returns the value as true
try
{
dbcontext.SaveChanges();
return true;
}
catch
{
return false;
}
}
Jignesh TrivediPosted Sep 3, 2014, 7:10 AM
Hi,
Please refer
http://www.codeproject.com/Articles/127395/Implementing-a-WCF-Service-with-Entity-Framework
In this article they show how to use EF with WCF... CRUD operation is same others
Hope this will help you.
Abhishek KumarPosted Sep 3, 2014, 6:39 AM
I have created a service using wcf and get data from database using entity frame work.
Please reference the service and use the methods you want to add.
Abhishek
Guest UserPosted Sep 3, 2014, 4:28 AM