2013-03-06 2 views
1

데이터베이스에서 데이터를 검색하는 두 가지 다른 방법이 있습니다. 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 프레임 워크의 문제점은 무엇입니까?

아무에게도 해결책을 제공 할 수 없습니까?

+0

jQuery의 오류 메서드에서 어떤 오류가 발생합니까? EF에 대해 –

+0

dt를 사용하고 있지 않습니다. 또한 그냥 "context.Customers.ToList()"를 반환해야합니다 – Aron

+0

Customer 클래스에 연관이 있습니까? 그것은 직렬화 문제 일 수 있습니다. 고객 유형이 EF 프록시 유형이 아닌 고객 유형인지 확인하십시오. 프록시 클래스 인 경우 CustomerEntities 객체의 구성에서 UseProxy를 해제해야합니다. – Aron

답변

0

고객 목록 수집을 비교하여 차이가 있는지 확인하십시오. 반환 객체 생성 문제 일 수 있습니다 ... 또는 사용 고객 이벤트에서 키워드 사용.

0

사용이

error: function (xhr, ajaxOptions, thrownError) { 
    alert(xhr.status); 
    alert(thrownError); 
    alert(xhr.responseText); 
    } 

그리고 오류가오고 무엇을 참조하십시오.
많은 오류가있을 수 있습니다.

+1

Xhr.status - 500 thrownError가 "내부 서버 오류"입니다. responseText에 " 'EF.Customer'유형의 개체를 직렬화하는 동안 순환 참조가 검색되었습니다." – Kokila

1

Entity Framework는 직렬화 할 수없는 개체를 만듭니다 (느린 로딩을 지원하므로 직렬화하면 데이터베이스가 직렬화 될 수 있습니다). 완전히로드 당신이 게으른을 해제해야이 동작을 중지하려면 데이터베이스 쿼리를하기 전에

  context.ContextOptions.ProxyCreationEnabled = false; 
      context.ContextOptions.LazyLoadingEnabled = false; 

이를 삽입하고, 문제는 아침에 내게 전화 지속될 경우.

+0

죄송합니다 ... 구성이 정의되지 않은 것처럼 코드를 구현하여 오류가 발생했습니다. 그런 다음 'ObjectContextOptions'클래스에 'ProxyCreationEnabled'속성이 있다는 것을 알았습니다. ObjectContextOptions cont = 새 ObjectContextOptions(); cont.ProxyCreationEnabled = false; cont.LazyLoadingEnabled = false; 하지만이 개체를 내 CustomerEntities 개체에 매핑하는 방법은 무엇입니까? 어떻게 둘 다 관련이 있을까요? – Kokila

+0

죄송합니다. EF5 DbContexts를 사용하고 있다고 가정했습니다. 업데이트 됨. – Aron

관련 문제