2013-04-18 2 views
0

먼저 내 질문을 읽어 주셔서 감사합니다.ADO.NET Entity Framework (Vs2012 + WCF) = 오류

저는 VS 2012를 사용하여 ADO.NET Entity Framework (5 가지로 생각합니다, 최신 버전)를 사용하여 솔루션을 개발 중입니다. 비즈니스 레이어로 WCF 서비스를 소개하기 전까지는 모두 잘 작동합니다 (학교용 과제이므로 비즈니스 레이어에서 WCF를 스크랩 할 수 없음).

문제는 데이터베이스에서 데이터를 요청할 때입니다. 데이터베이스에서 문자열을 반환하는 메서드가있을 때이 메서드는 기본 함수부터 잘 작동합니다. 하지만 Entity 객체 (예 : Account)를 반환하면 모두 지옥에갑니다.

예외 : (예, 매우 모호합니다.) 나는 [DataContract] [DataMember를] 속성을 추가 할 수 Entites.tt 파일을 수정 시도 : 나는 시도 무엇

An error occurred while receiving the HTTP response to http://localhost:8733/Services/AccountsManager. This could be due to the service endpoint binding not using the HTTP protocol. This could also be due to an HTTP request context being aborted by the server (possibly due to the service shutting down). See server logs for more details.

. 이것은 이전 버전의 EF에서는 자체적으로 수행하는 것처럼 보였기 때문입니다. 그러나 컴파일이 가능하고 직렬화가 불가능하다고 불평하지 않기 때문에 이것이 필요한지는 알 수 없습니다.

이것은 처음 모습입니다 :

namespace CommonLayer 
{ 
using System; 
using System.Collections.Generic; 

public partial class Account 
{ 
    public Account() 
    { 
     this.Transactions = new HashSet<Transaction>(); 
     this.Transactions1 = new HashSet<Transaction>(); 
    } 

    public System.Guid ID { get; set; } 
    public int Type { get; set; } 
    public string Name { get; set; } 
    public int Currency { get; set; } 
    public decimal Balance { get; set; } 
    public System.DateTime DateOpened { get; set; } 
    public Nullable<int> Duration { get; set; } 
    public string UserName { get; set; } 

    public virtual AccountType AccountType { get; set; } 
    public virtual Currency Currency1 { get; set; } 
    public virtual User User { get; set; } 
    public virtual ICollection<Transaction> Transactions { get; set; } 
    public virtual ICollection<Transaction> Transactions1 { get; set; } 
} 
} 

는 수정 후 보이는 방법 :

namespace CommonLayer 
{ 
using System; 
using System.Collections.Generic; 
using System.Runtime.Serialization; 
[DataContract] public partial class Account 
{ 
    public Account() 
    { 
     this.Transactions = new HashSet<Transaction>(); 
     this.Transactions1 = new HashSet<Transaction>(); 
    } 

    [DataMember] public System.Guid ID { get; set; } 
    [DataMember] public int Type { get; set; } 
    [DataMember] public string Name { get; set; } 
    [DataMember] public int Currency { get; set; } 
    [DataMember] public decimal Balance { get; set; } 
    [DataMember] public System.DateTime DateOpened { get; set; } 
    [DataMember] public Nullable<int> Duration { get; set; } 
    [DataMember] public string UserName { get; set; } 

    public virtual AccountType AccountType { get; set; } 
    public virtual Currency Currency1 { get; set; } 
    public virtual User User { get; set; } 
    public virtual ICollection<Transaction> Transactions { get; set; } 
    public virtual ICollection<Transaction> Transactions1 { get; set; } 
} 
} 

모든 포인터는 크게 감사하고 있습니다.

내 WCF 클래스

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.ServiceModel; 
using System.Text; 
using System.Threading.Tasks; 

namespace BusinessLayer 
{ 

    [ServiceContract] 
    interface IAccountsManager 
    { 
     [OperationContract] 
     List<CommonLayer.Account> GetAccounts(String UserName); 

     [OperationContract] 
     String GetData(); 

     [OperationContract] 
     CommonLayer.Account GetAccount(String UserName); 
    } 

    class AccountsManager: IAccountsManager, IDisposable 
    { 
     public List<CommonLayer.Account> GetAccounts(String UserName) 
     { 
      return DataLayer.AccountsRepository.Instance.GetAccountList(UserName).ToList(); 
     } 

     public String GetData() 
     { 
      CommonLayer.Account acc = this.GetAccounts("test").FirstOrDefault(); 
      return acc.DateOpened.ToString(); 
     } 

     public CommonLayer.Account GetAccount(String UserName) 
     { 
      return this.GetAccounts(UserName).FirstOrDefault(); 
     } 

     public void Dispose() 
     { 
      DataLayer.AccountsRepository.Reset(); 
     } 
    } 
} 
+0

질문을 편집하고 WCF 서비스 메서드를 추가 할 수 있습니까? –

+0

요청에 따라 수정 됨. 이러한 메서드에서 "GetData 메서드 작동". 다른 방법은 그렇지 않습니다. –

답변

0

당신은 DTO (데이터 전송 개체)를 사용하여 DTO에 EF 개체에서 매핑해야합니다.

[DataContract] 
public class MyDto { 
    [DataMember] 
    public int Id {get;set;} 
} 

및 방법

public static MyEntity Map(MyDto dto) { 
    return new MyEntity { Id = dto.Id }; 
} 

public static MyDto Map(MyEntity entity) { 
    return new MyDto { Id = entity.Id }; 
} 

서비스가 사용할 수 있도록 필요에 따라 다음 매핑 할 수있는 정적 매핑 클래스 :

그래서 서비스는 MyDto라는 개체가처럼 보이는 수용 할 수 DTO와 Entity Framework는 Entity를 사용할 수 있습니다.

0

EF 클래스에 클래스에 과 같은 탐색 속성이 있으면 WCF를 통해 전송 될 때 오류가 발생하는 것으로 보입니다.

많은 검색을 한 후 EF 클래스를 Navigation Properties와 생성자를 제외하고 EF 클래스와 동일한 DTO 클래스로 매핑하는 것 외에는 해결책을 찾지 못했습니다 (즉, ICollection과 같은 모든 가상 속성을 제거했습니다.)와 EF의 클래스 ctor에 새로운 클래스 내 EF 클래스 플러스 DTO 접미사 (예 : CustomerDto)와 같은라는 것을에서 만들었습니다.

나는 자동으로 DTO로의 EF 객체를 매핑 할 AutoMapper을 사용했습니다 동등한 것.g :

var customer = getCustomer(cid); 

    var customerDto = Mapper.Map<CustomerDto>(customer); 

    return customerDto; 

내 WCF 계약 또한 나는 매퍼 1 회 초기화를 할 필요가

[OperationContract] 
CustomerDto getCustomerData(int cid); 

이 포함되어 있습니다. 나는 Global.asax에서 다음과 같이했습니다.

Mapper.CreateMap<Customer, CustomerDto>(); 
0

내 Entities.Edmx 파일이 변경되었습니다. 첫째, 모든 (2) ".tt"파일을 삭제했습니다. 그런 다음 코드 생성 전략을 없음에서 기본값으로 변경했습니다. 이것은 내 모든 문제를 해결하는 것 같았다.

관련 문제