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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Ramesh MaruthiRank #937 · 2.2K reputationPosted Sep 3, 2014, 7:36 AM EDT
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;
}
}
Guest UserRank #259 · 9.8K reputationPosted Sep 3, 2014, 12:28 AM EDT
Abhishek KumarRank #510 · 4.8K reputationPosted Sep 3, 2014, 2:39 AM EDT
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
Jignesh TrivediRank #16 · 50.3K reputationPosted Sep 3, 2014, 3:10 AM EDT
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.