데이터베이스에서 데이터를 검색하는 두 가지 다른 방법이 있습니다. Entity Framework를 사용하는 코드가 jquery ajax의 성공 함수를 호출하지 않음
public List <Customer> GetDetail()
{
DataTable dt = new DataTable();
List<Customer> CustomerList = new List<Customer>();
CustomerEntities context = new CustomerEntities(GetConnectionObject());
foreach (Customer cus in context.Customers)
{
CustomerList.Add(cus);
}
return CustomerList;
}
엔티티 프레임 워크
를 사용public List<Customer> GetDetail()
{
SqlConnection connectionstring = new SqlConnection(connectionstring goes on..);
List<Customer> custList = new List<Customer>();
connectionstring.Open();
string query = "select * from Customer";
SqlCommand command = new SqlCommand(query, connectionstring);
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
Customer cust = new Customer();
cust.Name = reader["Name"].ToString();
cust.UserName = reader["UserName"].ToString();
cust.CountryId = reader["CountryId"].ToString();
cust.EmailId = reader["EmailId"].ToString();
cust.PhoneNo = reader["PhoneNo"].ToString();
custList.Add(cust);
}
}
connectionstring.Close();
return custList;
}
2 Ado.net
사용1)) 상기 메소드를 호출 JQuery와 Ajax 호출을 사용하여 제어 방법을 호출하고있다.
$.ajax({
type: "POST",
url: "/Customer/GetDetails",
dataType: 'json',
async: false,
cache: false,
success: function (data) {
alert("success");
$.each(data, function (index, customer) {
alert(customer.Name + " " + customer.UserName);
});
},
error: function (jqXHR, textStatus, errorThrown) {
if (typeof (console) != 'undefined') {
alert("oooppss");
}
else { alert("something went wrong"); }
}
});
정상적인 ado.net 코드 인 경우 데이터를 성공적으로 가져 와서 아약스 성공 함수가 호출됩니다.
그러나 엔티티 프레임 워크 메서드 인 경우 데이터를 다시 가져 오는 동안 (디버깅 중에 customerList 개체에서 결과 데이터를 볼 수 있음) 아약스 성공 함수가 호출되지 않습니다. 대신 오류 함수가 호출됩니다. errorThrown은 "내부 서버 오류"입니다.
왜? Entity 프레임 워크의 문제점은 무엇입니까?
아무에게도 해결책을 제공 할 수 없습니까?
jQuery의 오류 메서드에서 어떤 오류가 발생합니까? EF에 대해 –
dt를 사용하고 있지 않습니다. 또한 그냥 "context.Customers.ToList()"를 반환해야합니다 – Aron
Customer 클래스에 연관이 있습니까? 그것은 직렬화 문제 일 수 있습니다. 고객 유형이 EF 프록시 유형이 아닌 고객 유형인지 확인하십시오. 프록시 클래스 인 경우 CustomerEntities 객체의 구성에서 UseProxy를 해제해야합니다. – Aron