c# - 在 Entity Framework 6 中使用 Effort 的正确方法是什么?

标签 c# linq entity-framework entity-framework-6 effort

我正在尝试使用 Entity Framework 6 的 Effort 数据提供程序来运行一些测试代码。我正在尝试做的事情似乎应该是最简单的用例,但我就是做不到让一切正常运转。

这是我的 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] 属性是我定义的自定义属性,不应与 Entity Framework 有任何关系。

在我的单元测试中,我像这样设置数据连接,如 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 代码,我在这里缺少什么?

最佳答案

您不能在 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

关于c# - 在 Entity Framework 6 中使用 Effort 的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27177412/

相关文章:

c# - 如何为 List<T> 创建新的通用项?

c# - ASP.NET 中的 SetFocus() 和 Focus() 有什么区别?

c# - 使用 Mono 为 Linux 创建可执行文件?

c# - Entity Framework 中的 Datetime.UtcNow 查询的计算结果与 C# IL 中的 DateTime.UtcNow 不同

c# - Code First 问题,使用西里尔文播种数据

c# - XML 序列化 : System. InvalidOperationException:<tagname> 不是预期的

c# - LINQ 中的 GroupBy 是如何工作的?

c# - 我如何计算元素直到找到好的元素?

sql-server - SQL 的 "IN"关键字的 LINQ 等价物是什么

c# - EF lambda : How to fetch all data that match any id from a List