SCM

[#1011157] Fix (I think) for incorrect handling of timestamptz to DateTimeOffset mapping in Entity Framework Code First

View Trackers | Patches | Download .csv | Monitor

Date:
2012-02-02 05:36
Priority:
3
State:
Open
Submitted by:
Mirko Geffken (mirkogeffken)
Assigned to:
Nobody (None)
Category:
Group:
Resolution:
None
 
Summary:
Fix (I think) for incorrect handling of timestamptz to DateTimeOffset mapping in Entity Framework Code First

Detailed description
Method GetStoreType in NpgsqlProviderManifest currently handles both DateTime and DateTimeOffset types and attempts to map timestamp to DateTime and timestamptz to DateTimeOffset. The implementation for DateTime is correct, but for DateTimeOffset that is not the case in my opinion. The framework has both a CreateDateTimeTypeUsage as well as a CreateDateTimeOffsetTypeUsage function on the TypeUsage class and the incorrect one is called for DateTimeOffset.

Please find as attached file a new copy of NpgsqlProviderManifest.cs with the call corrected (based on currently latest source of 2.0.11.92).

Please note that I did not do a lot of testing on this change. It seems to resolve my immediate issue, but I cannot guarantee that it doesn't cause others.

Please note that without this change mappings such as this example:
[Table("Vendor", Schema = "Foo")]
public class Vendor
{
[Key]
public int ID { get; set; }
public string Name { get; set; }
[Column("JoinedOn")]
public DateTimeOffset JoinedOn { get; set; }
}

failed with something like this:
The PrimitiveType is not a DateTime type.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: The PrimitiveType is not a DateTime type.

Source Error:


Line 230: else
Line 231: {
Line 232: return TypeUsage.CreateDateTimeTypeUsage(StoreTypeNameToStorePrimitiveType["timestamptz"], null);
Line 233: }
Line 234: case PrimitiveTypeKind.Time:

Source File: C:\Users\Mirko\Downloads\Npgsql2.0.11.92.src\Npgsql2.0.11.92.src\src\Npgsql\NpgsqlProviderManifest.cs Line: 232

Stack Trace:


[ArgumentException: The PrimitiveType is not a DateTime type.]
System.Data.Metadata.Edm.TypeUsage.CreateDateTimeTypeUsage(PrimitiveType primitiveType, Nullable`1 precision) +7071757
Npgsql.NpgsqlProviderManifest.GetStoreType(TypeUsage edmType) in C:\Users\Mirko\Downloads\Npgsql2.0.11.92.src\Npgsql2.0.11.92.src\src\Npgsql\NpgsqlProviderManifest.cs:232
System.Data.Entity.ModelConfiguration.Edm.Services.StructuralTypeMappingGenerator.MapTableColumn(EdmProperty property, DbTableColumnMetadata tableColumnMetadata, Boolean isInstancePropertyOnDerivedType, Boolean isKeyProperty) +62
System.Data.Entity.ModelConfiguration.Edm.Services.PropertyMappingGenerator.Generate(EdmEntityType entityType, IEnumerable`1 properties, DbEntitySetMapping entitySetMapping, DbEntityTypeMappingFragment entityTypeMappingFragment, IList`1 propertyPath, Boolean createNewColumn) +1282
System.Data.Entity.ModelConfiguration.Edm.Services.EntityTypeMappingGenerator.Generate(EdmEntityType entityType, DbDatabaseMapping databaseMapping) +493
System.Data.Entity.ModelConfiguration.Edm.Services.DatabaseMappingGenerator.GenerateEntityTypes(EdmModel model, DbDatabaseMapping databaseMapping) +117
System.Data.Entity.ModelConfiguration.Edm.Services.DatabaseMappingGenerator.Generate(EdmModel model) +30
System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo) +189
System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection) +164
System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext) +62
System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input) +111
System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +416
System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +18
System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +62
System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext() +15
System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider() +37
System.Linq.Queryable.First(IQueryable`1 source) +50
PostgreSQLTest.Controllers.HomeController.Index() in C:\Users\Mirko\Documents\Projects\uurge\trunk\code\postgresqltest\code\PostgreSQLTest\Controllers\HomeController.cs:12
lambda_method(Closure , ControllerBase , Object[] ) +62
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +214
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +55
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +253
System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +21
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +191
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +326
System.Web.Mvc.Controller.ExecuteCore() +109
System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +91
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +10
System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +34
System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +21
System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +13
System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62
System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +48
System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +7
System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +22
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +60
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +10303829
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +178

Followup

Message
Date: 2012-02-02 13:48
Sender: Francisco Figueiredo jr.


Patch applied to cvs. Please give it a try and let me know if it works for you.

The patch was located in a cross reference bug comment: http://pgfoundry.org/tracker/?func=detail&atid=590&aid=1011102&group_id=1000140

Please, if possible, attach the original patch file.

This is the patch which was applied:

Index: src/Npgsql/NpgsqlProviderManifest.cs
===================================================================
RCS file: /cvsroot/npgsql/Npgsql2/src/Npgsql/NpgsqlProviderManifest.cs,v
retrieving revision 1.9
diff -u -r1.9 NpgsqlProviderManifest.cs
--- src/Npgsql/NpgsqlProviderManifest.cs 25 Jun 2010 01:38:01 -0000 1.9
+++ src/Npgsql/NpgsqlProviderManifest.cs 2 Feb 2012 13:40:49 -0000
@@ -225,11 +225,11 @@
if (edmType.Facets.TryGetValue(PrecisionFacet, false, out facet) &&
!facet.IsUnbounded && facet.Value != null)
{
- return TypeUsage.CreateDateTimeTypeUsage(StoreTypeNameToStorePrimitiveType["timestamptz"], (byte)facet.Value);
+ return TypeUsage.CreateDateTimeOffsetTypeUsage(StoreTypeNameToStorePrimitiveType["timestamptz"], (byte)facet.Value);
}
else
{
- return TypeUsage.CreateDateTimeTypeUsage(StoreTypeNameToStorePrimitiveType["timestamptz"], null);
+ return TypeUsage.CreateDateTimeOffsetTypeUsage(StoreTypeNameToStorePrimitiveType["timestamptz"], null);
}
case PrimitiveTypeKind.Time:
if (edmType.Facets.TryGetValue(PrecisionFacet, false, out facet) &&

Attached Files:

Changes:

No Changes Have Been Made to This Item

Powered By FusionForge