2013-01-09 23 views
7

मैं डेटाबेस पर एक कार्ट ऑब्जेक्ट सहेज रहा हूं जिसमें एक नाटकीय डेटाटाइम है।डेटाबेस में एक नामुमकिन डेटाटाइम डालने पर EF5 कोड पहले डेटाटाइम 2 का उपयोग क्यों करता है?

एक datetime डेटा प्रकार करने के लिए एक datetime2 डेटा प्रकार के रूपांतरण एक बाहर के रेंज मूल्य के परिणामस्वरूप: यह त्रुटि मैं हो रही है।

इस समस्या के समाधान को ठीक करने वाले कुछ स्टैक ओवरफ्लो पोस्ट हैं। हालांकि, जब कोड पहले डेटाबेस बना रहा है तो यह फ़ील्ड को डेटटाइम के रूप में बनाएगा (नल की अनुमति दें)। लेकिन किसी कारण से, कोड पहले डेटटाइम 2 फ़ील्ड का उपयोग करके सम्मिलित करने का प्रयास करता है।

मुझे आश्चर्य है कि क्यों ईएफ क्षेत्र को एक तरह से बनाता है, लेकिन उसी फ़ील्ड के लिए एक अलग प्रकार का उपयोग करके सम्मिलित करता है।

इस डोमेन वस्तु है:

using System; 
using System.Collections.Generic; 

namespace Core.Domain.Cart 
{ 
    public partial class Cart : BaseEntity, ILocalizedEntity 
    { 
     private ICollection<Catalog> _catalogs; 

     /// <summary> 
     /// Gets or sets the name 
     /// </summary> 
     public virtual string Name { get; set; } 

     /// <summary> 
     /// Gets or sets the zone identifier 
     /// </summary> 
     public virtual int ZoneId { get; set; } 

     /// <summary> 
     /// Gets or sets the brand identifier 
     /// </summary> 
     public virtual int BrandId { get; set; } 

     /// <summary> 
     /// Gets or sets the customer type identifier 
     /// </summary> 
     public virtual int CustomerTypeId { get; set; } 

     /// <summary> 
     /// Gets or sets the date and time of the opening of a cart 
     /// </summary> 
     public virtual DateTime? OpeningDateUtc { get; set; } 

     /// <summary> 
     /// Gets or sets the date and time of the closing of a cart 
     /// </summary> 
     public virtual DateTime? ClosingDateUtc { get; set; } 

     /// <summary> 
     /// Gets or sets a value indicating whether the entity is online or not 
     /// </summary> 
     public virtual bool IsOnline { get; set; } 

     /* Truncated for relevance */ 
    }  
} 

मॉडल:

using FluentValidation.Attributes; 
using System; 
using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations; 
using System.Web.Mvc; 
using Telerik.Web.Mvc; 


namespace Admin.Models.Cart 
{ 
     [Validator(typeof(CartValidator))] 
     public partial class CartModel : BaseNopEntityModel, ILocalizedModel<CartLocalizedModel> 
     {    
      public CartModel() 
      { 
       Locales = new List<CartLocalizedModel>(); 
       Catalogs = new List<CatalogModel>(); 
       UnassociatedCatalogs = new List<CatalogModel>(); 
      } 
      [NopResourceDisplayName("Admin.Carts.Fields.Name")] 
      [AllowHtml] 
      public string Name { get; set; } 

      //Zone dropdown 
      [NopResourceDisplayName("Admin.Carts.Fields.ZoneList")] 
      public SelectList ZoneList { get; set; }  //The dropdown with zones 
      public int ZoneId { get; set; }     //The selected value of the dropdown once the form is submitted 
      public string ZoneName { get; set; }   //The name of the zone to display in data-grid List view. 

      //Brand dropdown 
      [NopResourceDisplayName("Admin.Carts.Fields.BrandList")] 
      public SelectList BrandList { get; set; }  //The dropdown with brands 
      public int BrandId { get; set; }    //The selected value of the dropdown once the form is submitted 
      public string BrandName { get; set; }   //The name of the brand to display in the data-grid List view. 

      //Customer type dropdown 
      [NopResourceDisplayName("Admin.Carts.Fields.CustomerTypeList")] 
      public SelectList CustomerTypeList { get; set; }//The dropdown with CustomerType 
      public int CustomerTypeId { get; set; }   //The selected value of the dropdown once the form is submitted 
      public string CustomerTypeName { get; set; } //The name of the CustomerType to display in the data-grid List view. 

      [NopResourceDisplayName("Admin.Carts.Fields.OpeningDateUtc")] 
      [UIHint("DateNullable")] 
      public DateTime? OpeningDateUtc { get; set; } 

      [NopResourceDisplayName("Admin.Carts.Fields.ClosingDateUtc")] 
      [UIHint("DateNullable")] 
      public DateTime? ClosingDateUtc { get; set; } 

      [NopResourceDisplayName("Admin.Carts.Fields.IsOnline")] 
      public bool IsOnline { get; set; } 

      /* Truncated for relevance */ 
     } 

} 

तो दोनों OpeningDateUtc और ClosingDateUtc प्रकार दिनांक समय की हैं ?.

यह कैसे डेटाबेस एफई कोड पहले द्वारा उत्पन्न हो जाता है: EF generated table

OpeningDateUtc और ClosingDateUtc एक नल दिनांक समय क्षेत्र के रूप में बनाया जाता है।

तो क्यों यह है कि जब मैं IDBContext.SaveChanges() का उपयोग कर बचाने के लिए, क्वेरी के लिए उत्पन्न एसक्यूएल है:

exec sp_executesql N'update [dbo].[Cart] 
set [Name] = @0, [ZoneId] = @1, [BrandId] = @2, [CustomerTypeId] = @3, [OpeningDateUtc] = @4, [ClosingDateUtc] = @5, [IsOnline] = @6, [IsReadonly] = @7, [IsPreviewMode] = @8, [CreatedOnUtc] = @9 
where ([Id] = @10) 
',N'@0 nvarchar(100),@1 int,@2 int,@3 int,@4 datetime2(7),@5 datetime2(7),@6 bit,@7 bit,@8 bit,@9 datetime2(7),@10 int',@0=N'Cart1',@1=7,@2=4,@3=5,@4='2013-01-09 00:00:00',@5='2013-01-18 00:00:00',@6=0,@7=0,@8=1,@9='0001-01-01 00:00:00',@10=1 

दिलचस्प हिस्सा होने @4 datetime2(7),@5 datetime2(7)

मुझे समझ में आता है कि मैं .HasColumnType("datetime2") कार्ट मानचित्र में जोड़कर इस समस्या को ठीक कर सकता हूं, लेकिन यह जवाब नहीं देता है कि क्यों EF5 (और शायद पुराने संस्करण) उन्हें निष्क्रिय समय पर सेट कर देते हैं।

उत्तर

15

DateTime टाइप में .NET में SQL Server में datetime2 के समान श्रेणी और परिशुद्धता है। जब ईएफ SQL सर्वर में datetime या datetime2 कॉलम को सम्मिलित करता है या अपडेट करता है तो यह मॉडल प्रॉपर्टी को उस प्रकार में परिवर्तित करता है जो DateTime की संपूर्ण श्रेणी .NET में रख सकता है, जो datetime2 है। datetime में कनवर्ट करना विफल होगा यदि DateTime संपत्ति SQL सर्वर में datetime की सीमा के अंदर नहीं है।

समस्या यह है कि, अपवाद रहे हैं का कारण बनता है जिस तरह से, नहीं दो नल OpeningDateUtc और ClosingDateUtc स्तंभों के आधार पर है, लेकिन CreatedOnUtc मूल्य जो अपने एसक्यूएल स्निपेट में '0001-01-01 00:00:00' है, यानि कि CreatedOnUtc जाहिरा तौर पर अपने मॉडल इकाई में आरंभ नहीं किया है। एसक्यूएल सर्वर में datetime की सबसे पुरानी तारीख सालाना 1750 में स्टोर कर सकती है, इसलिए वर्ष 0001 इस प्रकार फिट नहीं होगा (लेकिन यह datetime2 में फिट होगा)।

तो, समाधान या तो एक वैध datetime मूल्य के लिए CreatedOnUtc सेट या करने के लिए है - जैसा कि आप जानते - datetime2 के रूप में प्रकार को परिभाषित अपनी मैपिंग में।

लेकिन मैं मानता हूं कि अगर ईएफ DateTime गुणों को datetime2 पर डिफ़ॉल्ट रूप से मैप करेगा तो कम भ्रम होगा।

+0

धन्यवाद, मुझे समाधान मिला है। –

11

ईएफ टीम ने वास्तव में डिज़ाइन मीटिंग में से किसी एक के दौरान इस विशेष आइटम पर चर्चा की। निर्णय मौजूदा व्यवहार को छोड़ना था। यहां meeting notes हैं जो आपको अधिक संदर्भ दे सकते हैं।

+0

उत्कृष्ट पढ़ा, धन्यवाद! –

+0

@MattR जानकारी के लिए धन्यवाद –