在實體框架6中使用Effort的正確方法是什麼?

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

我正在嘗試使用帶有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;

您可以在此處獲得一些信息: 在此上下文中僅支持原始類型或枚舉類型



Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因