2012-12-13 4 views
1

는 다음과 같은 엔티티 클래스가 I :탐색 속성으로 쿼리하는 중 오류가 발생 했습니까?

using System; 
using System.Collections.Generic; 
using Facebook.Business.Domain.Accounts; 

namespace Business.Domain.Posts 
{ 
    /// <summary> 
    /// This class defines a post. 
    /// </summary> 
    public class Post : Entity<Post> 
    { 
     #region Fields 

     private UserAccount _author; 
     private UserAccount _owner; 
     private DateTime _date; 
     private string _text; 
     private IList<Comment> _comments; 

     #endregion 

     #region Ctors 

     protected Post() 
     { 
     } 

     public Post(UserAccount author, 
        UserAccount owner, 
        DateTime date, 
        string text) 
     { 
      _author = author; 
      _owner = owner; 
      _date = date; 
      _text = text; 
      _comments = new List<Comment>(); 
     } 

     #endregion 

     /// <summary> 
     /// Gets or sets the account that writes and sends the current post. 
     /// </summary> 
     public virtual UserAccount Author 
     { 
      get { return _author; } 
      protected set { _author = value; } 
     } 

     /// <summary> 
     /// Gets or sets the datetime of the current post. 
     /// </summary> 
     public virtual DateTime Date 
     { 
      get { return _date; } 
      protected set { _date = value; } 
     } 

     /// <summary> 
     /// Gets or sets the text of the current post. 
     /// </summary> 
     public virtual string Text 
     { 
      get { return _text; } 
      protected set { _text = value; } 
     } 

     /// <summary> 
     /// Gets or sets the user that receives the current post. 
     /// </summary> 
     public virtual UserAccount Owner 
     { 
      get { return _owner; } 
      protected set { _owner = value; } 
     } 

     /// <summary> 
     /// Gets or sets the comments for the current post. 
     /// </summary> 
     public virtual IList<Comment> Comments 
     { 
      get { return _comments; } 
      protected set { _comments = value; } 
     } 

     public void Add(Comment comment) 
     { 
      _comments.Add(comment); 
     } 

     #region Implementation of ICommentable 

     IEnumerable<Comment> ICommentable.Comments { get { return Comments; } } 

     #endregion 
    } 
} 

클래스 UserAccount는 포스트 클래스에 대해 아무것도 몰라를 UserAccount 또한 엔티티입니다.

는 나는 각각의 저장소 클래스에서이 메소드를 구현해야합니다

/// <summary> 
/// Retrieve the posts that were posted by a given account: <paramrefname="author"/> 
/// </summary> 
/// <param name="author">An account.</param> 
/// <returns>The posts that were posted by <paramref name="author"/></returns> 
public IQueryable<Post> FindPostsFrom(UserAccount author) 
{ 
    ContractUtil.NotNull(author); 

    return Set.Where(post => post.Author.Equals(author)); 
} 

을하지만 난 내가 다음과 같은 오류를 얻을 테스트를 실행하면

System.NotSupportedException : Unable to create a constant value of type 'Facebook.Business.Domain.Accounts.UserAccount'. Only primitive types or enumeration types are supported in this context.

... 그리고 스택. ..

at System.Data.Objects.ELinq.ExpressionConverter.ConstantTranslator.TypedTranslate(ExpressionConverter parent, ConstantExpression linq) at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) at System.Data.Objects.ELinq.ExpressionConverter.EqualsTranslator.TypedTranslate(ExpressionConverter parent, BinaryExpression linq) at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) at System.Data.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda, DbExpression input) at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, ref DbExpression source, ref DbExpressionBinding sourceBinding, ref DbExpression lambda) at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call) at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq) at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.UnarySequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call) at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq) at System.Data.Objects.ELinq.ExpressionConverter.Convert() at System.Data.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable 1 forMergeOption) at System.Data.Objects.ObjectQuery 1.GetResults(Nullable 1 forMergeOption) at System.Data.Objects.ObjectQuery 1.System.Collections.Generic.IEnumerable.GetEnumerator() at System.Linq.Enumerable.First(IEnumerable 1 source) at System.Linq.Queryable.First(IQueryable 1 source) at Data.EntityFramework.Tests.PostRepositoryTests.FindPostsFrom_FindingAPostGivenAnAuthor_ShouldRetrieveIt() in PostRepositoryTests.cs: line 136

지금까지 아무도 내가 무슨 일을하고 있는지 설명 할 수 있습니까? 쿼리에 탐색 속성을 사용해서는 안됩니다.

아무에게도 대안 솔루션을 제공 할 수 있습니까?

미리 감사드립니다.

답변

1

Entity Framework 쿼리는 항상 술어의 프리미티브 속성을 사용해야합니다. 그래서 당신은 (dutzu에서 알 수 있듯이)

Set.Where(post => post.Author.Id == author.Id); 

또는을 Post의 속성으로 AuthorId 노출 될 수 있습니다. 그렇게한다면, AuthorIdAuthor이 둘 다 같은 외래 키의 일부라는 것을 EF에 말해야 할 것입니다. 그것은 (ObjectContext 또는 DbContext) 사용하는 API에 따라 다릅니다.

EF가 너무 엄격한 이유는 (더 많을 수도 있음) 값 유형이나 클래스의 동등성에 대한 가정을 할 수 없기 때문입니다. EF는 POCO로 작업 할 수 있으므로 Equals() 또는 == 연산자는 키 값의 동일성을 자동으로 평가하지 않습니다. POCO는 키로 동등 함을 보장하기 위해 명시 적으로 IEquatable을 구현해야합니다. 반면에 원시 값의 평등은 항상 정의됩니다.

1

그들은 관련 엔티티이며 관계는 각 게시물에 authorId의 값을 유지하는 필드가 있음을 의미하는 일대 다 (한 명의 작성자는 많은 게시물이 있음)입니다.

post.authorId = author.Id 인 게시물을 검색하도록 쿼리를 다시 작성할 수 있습니다.

관련 문제