【发布时间】:2012-12-13 13:49:12
【问题描述】:
我有以下实体类:
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 类对 Post 类一无所知,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:无法创建常量值 输入“Facebook.Business.Domain.Accounts.UserAccount”。只有原始 在此上下文中支持类型或枚举类型。
...和堆栈...
在 System.Data.Objects.ELinq.ExpressionConverter.ConstantTranslator.TypedTranslate(ExpressionConverter 父级,ConstantExpression linq) 在 System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) 在 System.Data.Objects.ELinq.ExpressionConverter.EqualsTranslator.TypedTranslate(ExpressionConverter parent, BinaryExpression linq) 在 System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) 在 System.Data.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda, DbExpression 输入) 在 System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent,MethodCallExpression call,ref DbExpression source,ref DbExpressionBinding sourceBinding,ref DbExpression lambda) 在 System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call) 在 System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter 父级,MethodCallExpression linq) 在 System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) 在 System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.UnarySequenceMethodTranslator.Translate(ExpressionConverter 父级,MethodCallExpression 调用) 在 System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter 父级,MethodCallExpression linq) 在 System.Data.Objects.ELinq.ExpressionConverter.Convert() 在 System.Data.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable
1 forMergeOption) at System.Data.Objects.ObjectQuery1.GetResults(Nullable1 forMergeOption) at System.Data.Objects.ObjectQuery1.System.Collections.Generic.IEnumerable.GetEnumerator() 在 System.Linq.Enumerable.First(IEnumerable1 source) at System.Linq.Queryable.First(IQueryable1 源) 在 Data.EntityFramework.Tests.PostRepositoryTests.FindPostsFrom_FindingAPostGivenAnAuthor_ShouldRetrieveIt() 在 PostRepositoryTests.cs:第 136 行
到目前为止,谁能解释一下这里发生了什么? 似乎是我不应该使用导航属性来查询。
谁能给我一个替代解决方案?
提前致谢。
【问题讨论】:
标签: c# entity-framework