How do I resolve issues with unsupported DateTimeO

2019-04-16 17:03发布

问题:

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

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

I am using Entity Framework version 6 on SQL Server.

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,  

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:

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.

回答1:

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

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.