我在EF 5中使用WCF
Restful服务。
当我想将记录插入数据库时,它会给我错误
The server encountered an error processing the request. See server logs for more details.
为此,我找到了这些解决方案
WCF Data Service 5.6 quick start
The server encountered an error processing the request
根据这个解决方案,我已经更改了我的代码,但仍然遇到了同样的问题。这是我的更改服务类代码
namespace WcfRestMVC
{
public class EmployeeService : DataService<DemoContext>, IEmployeeService
{
public string AddEmployee(Employee employee)
{
if (employee != null)
{
return "Records Could not be added";
}
else
{
using (DemoContext DbObj = new DemoContext())
{
DbObj.employees.Add(employee);
DbObj.SaveChanges();
return "Employee Added";
}
}
}
}
}
和我的界面如下
namespace WcfRestMVC
{
[ServiceContract]
public interface IEmployeeService
{
[OperationContract]
[WebInvoke(ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, Method = "POST", UriTemplate = "AddEmployee")]
string AddEmployee(Employee employee);
}
}
这是我的上下文类
public class DemoContext:DbContext
{
public DbSet<Employee> employees { get; set; }
}
我的JS
$('#btnAdd').on('click', function () {
var Fname = $('#txtFname').val();
var Lname = $('#txtLname').val();
var Title = $('#txtTitle').val();
var employeeData = {
"FirstName": Fname,
"LastName": Lname,
"Title": Title
};
$.ajax({
type: "POST",
url: "http://localhost:53125/EmployeeService.svc/AddEmployee",
contentType: "json",
dataType: "json",
data: JSON.stringify(employeeData),
processData: true,
success: function (data) {
$('#spanAdd').text(data);
$('#spanAdd').show();
BindData();
},
error: function (xhr) {
alert(xhr.responseText);
}
});
});