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

查看:443
本文介绍了如何使用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上使用Entity Framework版本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中的

(String storeTypeName,String nameForException,PrimitiveTypeKind nativeTypeKind) 在System.Data.Entity.SqlServer.SqlProviderManifest.GetStoreType(TypeUsage edmType) 在System.Data.Entity.ModelConfiguration.Edm.Services.StructuralTypeMappingGenerator.MapTableColumn(EdmProperty属性,字符串columnName,布尔值isInstancePropertyOnDerivedType) 在System.Data.Entity.ModelConfiguration.Edm.Services.PropertyMappingGenerator.Generate(EntityType实体类型,IEnumerable 1 properties, EntitySetMapping entitySetMapping, MappingFragment entityTypeMappingFragment, IList 1 propertyPath,布尔createNewColumn) 在System.Data.Entity.ModelConfiguration.Edm.Services.TableMappingGenerator.Generate(EntityType实体类型,DbDatabaseMapping数据库映射) 在System.Data.Entity.ModelConfiguration.Edm.Services.DatabaseMappingGenerator.GenerateEntityTypes(DbDatabaseMapping databaseMapping) 在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天全站免登陆