如何使用 Sql Server 解决 EF 6 中不受支持的 DateTimeOffsets 的问题? [英] How do I resolve issues with unsupported DateTimeOffsets in EF 6 using Sql Server?

查看:36
本文介绍了如何使用 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 的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆