我试图使用带有实体框架6的Effort数据提供程序来使用一些测试代码。我尝试做的事情似乎应该是绝对最简单的用例,但我&#39 ;我只是无法让事情发挥作用。
这是我的DbContext类:
public class CcdReductionFrameCatalogue : DbContext
{
public CcdReductionFrameCatalogue()
: this("name=CcdReductionFrameCatalogue") {}
public CcdReductionFrameCatalogue(string connectionString) : base(connectionString) {}
public CcdReductionFrameCatalogue(DbConnection connection) : base(connection, true) {}
public virtual DbSet<CcdFrame> CcdFrames { get; set; }
}
POCO实体定义为:
public class CcdFrame : IEquatable<CcdFrame>
{
public CcdFrame()
{
AcquisitionTimeUtc = DateTime.UtcNow;
}
public int Id { get; set; }
public string Location { get; set; }
[FitsKeyword("INSTRUME")] public string CameraName { get; set; }
[FitsKeyword("EXPTIME")] public double ExposureTimeSeconds { get; set; }
[FitsKeyword("SET-TEMP"), FitsKeyword("CCD-TEMP")] public double TemperatureSetpoint { get; set; }
[FitsKeyword("NAXIS1")] public int SizeX { get; set; }
[FitsKeyword("NAXIS2")] public int SizeY { get; set; }
[FitsKeyword("XBINNING")] public int BinningX { get; set; }
[FitsKeyword("YBINNING")] public int BinningY { get; set; }
[FitsKeyword("IMAGETYP")] public string FrameType { get; set; }
[FitsKeyword("DATE-OBS")] public DateTime AcquisitionTimeUtc { get; set; }
[FitsKeyword("XORGSUBF")] public int SubframeOriginX { get; set; }
[FitsKeyword("YORGSUBF")] public int SubframeOriginY { get; set; }
// IEquatable implementation elided for clarity
}
[FitsKeyword]
属性是我定义的自定义属性,不应对实体框架产生任何影响。
在我的单元测试中,我设置了这样的数据连接,如Effort快速入门指南中所示:
Connection = DbConnectionFactory.CreateTransient();
Repository = new CcdReductionFrameCatalogue(Connection);
一旦我在DbSet上使用任何LINQ,我就会得到愚蠢无意义的错误消息。例如,当我将我的存储库传递给这个简单的代码时:
static void AddOrUpdate(CcdFrame newFrame, CcdReductionFrameCatalogue repository)
{
var existingFrames = from frame in repository.CcdFrames
where frame.Equals(newFrame)
select frame;
Console.WriteLine(existingFrames.Count());
// ...never gets past here
当我运行此代码时,我得到:
System.NotSupportedExceptionUnable to create a constant value of type 'TA.ReductionManager.DomainObjects.CcdFrame'. Only primitive types or enumeration types are supported in this context. at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.ConstantTranslator.TypedTranslate(ExpressionConverter parent, ConstantExpression linq) at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.EqualsTranslator.TypedTranslate(ExpressionConverter parent, BinaryExpression linq) at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda, DbExpression input) at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, ref DbExpression source, ref DbExpressionBinding sourceBinding, ref DbExpression lambda) at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call) at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq) at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq) at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.AggregateTranslator.Translate(ExpressionConverter parent, MethodCallExpression call) at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq) at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.Convert() at System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption) at System.Data.Entity.Core.Objects.ObjectQuery`1.c__DisplayClass7.b__6() at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction(Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess) at System.Data.Entity.Core.Objects.ObjectQuery`1.c__DisplayClass7.b__5() at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption) at System.Data.Entity.Core.Objects.ObjectQuery`1..GetEnumerator>b__0() at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext() at System.Linq.Enumerable.Single(IEnumerable`1 source) at System.Linq.Queryable.Count(IQueryable`1 source) at TA.ReductionManager.Specifications.FitsImporter.AddOrUpdate(CcdFrame newFrame, CcdReductionFrameCatalogue repository) in FitsImporter.cs: line 38 at TA.ReductionManager.Specifications.FitsImporter.ImportCollection(IEnumerable`1 collection, CcdReductionFrameCatalogue repository) in FitsImporter.cs: line 29 at TA.ReductionManager.Specifications.when_importing_fits_files_into_the_catalogue_and_there_are_no_subdirectories.b__5() in FileEnumeratorSpecs.cs: line 27
现在这是一个令人难以置信的简单LINQ代码,我在这里缺少什么?
答案 0 :(得分:4)
您无法比较LINQ查询中的自定义对象。您应该只比较基本类型(int,string等)。
var existingFrames = from frame in repository.CcdFrames
where frame.Id == newFrame.Id
select frame;
您可以在此处获取一些信息:Only primitive types or enumeration types are supported in this context