Skip to main content
 首页 » 编程设计

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

2024年02月06日12jiqing9006

我正在尝试使用 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