如何使用 Sql Server 解决 EF 6 中不受支持的 DateTimeOffsets 的问题?

How do I resolve issues with unsupported DateTimeOffsets in EF 6 using Sql Server?(如何使用 Sql Server 解决 EF 6 中不受支持的 DateTimeOffsets 的问题?)
本文介绍了如何使用 Sql Server 解决 EF 6 中不受支持的 DateTimeOffsets 的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试实例化 DbContext 时,我收到以下消息:

When I try to instantiate a DbContext I receive this message:

System.NotSupportedException:没有对应于原始类型DateTimeOffset"的概念侧类型DateTimeOffset"的存储类型.

System.NotSupportedException: There is no store type corresponding to the conceptual side type 'DateTimeOffset' of primitive type 'DateTimeOffset'.

我在 SQL Server 上使用实体框架版本 6.

I am using Entity Framework version 6 on SQL Server.

DbContext 的构造函数(带有引发异常的行)如下所示:

The constructor of the DbContext (with the line that throws the exception) looks like this:

    internal TestHubContext(string connectionStringName) : base(connectionStringName)
    {
        var objectContext = (this as IObjectContextAdapter).ObjectContext;

        ...
    }

实体是使用代码优先创建的,如下所示:

The entities are created using code-first, and look like this:

public class Order
{
    [Key]
    [Required]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public System.Guid Id { get; set; }

    [MaxLength(60)]
    [Required]
    public string CreatedBy { get; set; }

    [Required]
    public System.DateTimeOffset CreatedUtcDate { get; set; }
}

数据库迁移运行良好并生成了如下表:

The database migrations ran fine and generated a table like this:

CREATE TABLE [dbo].[Orders](
[Id] [uniqueidentifier] NOT NULL,   
[CreatedBy] [nvarchar](60) NOT NULL,
[CreatedUtcDate] [datetimeoffset](7) NOT NULL,  

所以相关的数据类型存在于数据库和C#代码中.为什么会这样,我有点茫然.

So the relevant data types exist in the database and the C# code. I am a bit of a loss as to why this is happening.

这是我得到的堆栈跟踪:

This is the stack trace I am getting:

在 System.Data.Entity.SqlServer.SqlProviderManifest.GetStorePrimitiveTypeIfPostSql9(字符串 storeTypeName,字符串 nameForException,PrimitiveTypeKind primitiveTypeKind)在 System.Data.Entity.SqlServer.SqlProviderManifest.GetStoreType(TypeUsage edmType)在 System.Data.Entity.ModelConfiguration.Edm.Services.StructuralTypeMappingGenerator.MapTableColumn(EdmProperty 属性,字符串列名,布尔 isInstancePropertyOnDerivedType)在 System.Data.Entity.ModelConfiguration.Edm.Services.PropertyMappingGenerator.Generate(EntityType entityType, IEnumerable1 属性, EntitySetMapping entitySetMapping, MappingFragment entityTypeMappingFragment, IList1 propertyPath, Boolean createNewColumn)在 System.Data.Entity.ModelConfiguration.Edm.Services.TableMappingGenerator.Generate(EntityType entityType,DbDatabaseMapping databaseMapping)在 System.Data.Entity.ModelConfiguration.Edm.Services.DatabaseMappingGenerator.GenerateEntityTypes(DbDatabaseMapping 数据库映射)在 System.Data.Entity.ModelConfiguration.Edm.Services.DatabaseMappingGenerator.Generate(EdmModel 概念模型)在 System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest,DbProviderInfo providerInfo)在 System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)在 System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)在 System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput 输入)在 System.Data.Entity.Internal.LazyInternalContext.InitializeContext()在 System.Data.Entity.Internal.InternalContext.ForceOSpaceLoadingForKnownEntityTypes()在 System.Data.Entity.DbContext.System.Data.Entity.Infrastructure.IObjectContextAdapter.get_ObjectContext()

at System.Data.Entity.SqlServer.SqlProviderManifest.GetStorePrimitiveTypeIfPostSql9(String storeTypeName, String nameForException, PrimitiveTypeKind primitiveTypeKind) at System.Data.Entity.SqlServer.SqlProviderManifest.GetStoreType(TypeUsage edmType) at System.Data.Entity.ModelConfiguration.Edm.Services.StructuralTypeMappingGenerator.MapTableColumn(EdmProperty property, String columnName, Boolean isInstancePropertyOnDerivedType) at System.Data.Entity.ModelConfiguration.Edm.Services.PropertyMappingGenerator.Generate(EntityType entityType, IEnumerable1 properties, EntitySetMapping entitySetMapping, MappingFragment entityTypeMappingFragment, IList1 propertyPath, Boolean createNewColumn) at System.Data.Entity.ModelConfiguration.Edm.Services.TableMappingGenerator.Generate(EntityType entityType, DbDatabaseMapping databaseMapping) at System.Data.Entity.ModelConfiguration.Edm.Services.DatabaseMappingGenerator.GenerateEntityTypes(DbDatabaseMapping databaseMapping) at System.Data.Entity.ModelConfiguration.Edm.Services.DatabaseMappingGenerator.Generate(EdmModel conceptualModel) at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo) at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection) at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext) at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input) at System.Data.Entity.Internal.LazyInternalContext.InitializeContext() at System.Data.Entity.Internal.InternalContext.ForceOSpaceLoadingForKnownEntityTypes() at System.Data.Entity.DbContext.System.Data.Entity.Infrastructure.IObjectContextAdapter.get_ObjectContext()

如果您有任何解决此问题的方法的想法,我将不胜感激,而不必破解数据类型.

I would be grateful for any ideas of ways to resolve this, without having to hack the datatypes.

推荐答案

Facepalm!此问题是由配置文件中的连接字符串名称拼写错误引起的.

Facepalm! This issue was caused by a typo in the name of the connection string in the config file.

一旦我确定配置中的连接字符串的名称与提供给 DbContext 的名称匹配,问题就消失了.

Once I made sure the name of the connection string in the config matched the one supplied to the DbContext the problem disappeared.

当然,抛出的异常并没有帮助我.

Of course, I was not helped by the exception that was thrown.

我在这里列出这个作为答案,以便将来其他人可能会找到这个答案,或者至少知道这种异常可以在各种情况下引发.

I'm listing this here as an answer so that someone else in the future might find this answer, or at the very least know that this kind of exception can be thrown in a variety of situations.

这篇关于如何使用 Sql Server 解决 EF 6 中不受支持的 DateTimeOffsets 的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)