0

我目前在从实体框架更新 NopCommerce 数据库时遇到了一个非常烦人的问题。

背景:

我正在编写一个小工具来将一种过时的电子商务应用程序迁移到 nopCommerce。

错误:

The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.
The statement has been terminated.

我做了什么来尝试解决它

过去,当我有一个未初始化的 DateTime 属性(0001 年)时,就会出现这种错误。但在这种情况下,这不是问题所在。我查看并确保正在设置所有 DateTime 属性。由于那不起作用,我也尝试制作 db 列datetime2(7),但没有运气。最后我将它设置为NULLABLE但它仍然给了我同样的错误。因此,显然这里发生了其他事情,EF 抛出了一些无用的错误。我想知道表上是否可能有一个插入触发器(或任何触发器)试图在产品变体表或另一个表中创建一个条目,然后日期时间错误来自该表,但这不是案子。

编码:

foreach (var piloProduct in piloProducts)
{
    var product = new Models.Nop.Product
    {
        Name = piloProduct.Name,
        ShortDescription = piloProduct.ShortDescription,
        FullDescription = piloProduct.FullDescription,
        ProductTemplateId = 1,
        ShowOnHomePage = false,
        MetaKeywords = piloProduct.MetaKeywords,
        MetaDescription = piloProduct.MetaDescription,
        MetaTitle = piloProduct.MetaTitle,
        SeName = piloProduct.SeName,
        AllowCustomerReviews = false,
        ApprovedRatingSum = 0,
        NotApprovedRatingSum = 0,
        ApprovedTotalReviews = 0,
        NotApprovedTotalReviews = 0,
        Published = true,
        Deleted = false,
        CreatedOnUtc = DateTime.UtcNow,
        UpdatedOnUtc = DateTime.UtcNow
    };
    nopDb.Products.AddObject(product);
}
nopDb.SaveChanges();

我对这个完全不知所措,因为上面的代码非常简单明了。非常感谢任何帮助。

更新

我刚刚尝试使用 Linq-To-SQL 类而不是实体框架 EDMX,但遇到了完全相同的问题。奇怪的是,我直接从 SQL Management Studio 在表上运行了一个“INSERT INTO”语句,但它起作用了.. 去看看......有什么方法可以准确地查看 EF 或生成的“INSERT”语句Linq 到 SQL?

这是 Mark Oreta 要求的 EDMX 生成模型:

[EdmEntityTypeAttribute(NamespaceName="eSalesModel", Name="Product")]
[Serializable()]
[DataContractAttribute(IsReference=true)]
public partial class Product : EntityObject
{
    public static Product CreateProduct(global::System.Int32 id, global::System.String name, global::System.Int32 productTemplateId, global::System.Boolean showOnHomePage, global::System.Boolean allowCustomerReviews, global::System.Int32 approvedRatingSum, global::System.Int32 notApprovedRatingSum, global::System.Int32 approvedTotalReviews, global::System.Int32 notApprovedTotalReviews, global::System.Boolean published, global::System.Boolean deleted)
    {
        Product product = new Product();
        product.Id = id;
        product.Name = name;
        product.ProductTemplateId = productTemplateId;
        product.ShowOnHomePage = showOnHomePage;
        product.AllowCustomerReviews = allowCustomerReviews;
        product.ApprovedRatingSum = approvedRatingSum;
        product.NotApprovedRatingSum = notApprovedRatingSum;
        product.ApprovedTotalReviews = approvedTotalReviews;
        product.NotApprovedTotalReviews = notApprovedTotalReviews;
        product.Published = published;
        product.Deleted = deleted;
        return product;
    }
[EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
[DataMemberAttribute()]
public global::System.Int32 Id
{
    get
    {
        return _Id;
    }
    set
    {
        if (_Id != value)
        {
            OnIdChanging(value);
            ReportPropertyChanging("Id");
            _Id = StructuralObject.SetValidValue(value);
            ReportPropertyChanged("Id");
            OnIdChanged();
        }
    }
}
private global::System.Int32 _Id;
partial void OnIdChanging(global::System.Int32 value);
partial void OnIdChanged();

[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
[DataMemberAttribute()]
public Nullable<global::System.Int32> PiloId
{
    get
    {
        return _PiloId;
    }
    set
    {
        OnPiloIdChanging(value);
        ReportPropertyChanging("PiloId");
        _PiloId = StructuralObject.SetValidValue(value);
        ReportPropertyChanged("PiloId");
        OnPiloIdChanged();
    }
}
private Nullable<global::System.Int32> _PiloId;
partial void OnPiloIdChanging(Nullable<global::System.Int32> value);
partial void OnPiloIdChanged();

[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
[DataMemberAttribute()]
public global::System.String Name
{
    get
    {
        return _Name;
    }
    set
    {
        OnNameChanging(value);
        ReportPropertyChanging("Name");
        _Name = StructuralObject.SetValidValue(value, false);
        ReportPropertyChanged("Name");
        OnNameChanged();
    }
}
private global::System.String _Name;
partial void OnNameChanging(global::System.String value);
partial void OnNameChanged();

[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
[DataMemberAttribute()]
public global::System.String ShortDescription
{
    get
    {
        return _ShortDescription;
    }
    set
    {
        OnShortDescriptionChanging(value);
        ReportPropertyChanging("ShortDescription");
        _ShortDescription = StructuralObject.SetValidValue(value, true);
        ReportPropertyChanged("ShortDescription");
        OnShortDescriptionChanged();
    }
}
private global::System.String _ShortDescription;
partial void OnShortDescriptionChanging(global::System.String value);
partial void OnShortDescriptionChanged();

[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
[DataMemberAttribute()]
public global::System.String FullDescription
{
    get
    {
        return _FullDescription;
    }
    set
    {
        OnFullDescriptionChanging(value);
        ReportPropertyChanging("FullDescription");
        _FullDescription = StructuralObject.SetValidValue(value, true);
        ReportPropertyChanged("FullDescription");
        OnFullDescriptionChanged();
    }
}
private global::System.String _FullDescription;
partial void OnFullDescriptionChanging(global::System.String value);
partial void OnFullDescriptionChanged();

[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
[DataMemberAttribute()]
public global::System.String AdminComment
{
    get
    {
        return _AdminComment;
    }
    set
    {
        OnAdminCommentChanging(value);
        ReportPropertyChanging("AdminComment");
        _AdminComment = StructuralObject.SetValidValue(value, true);
        ReportPropertyChanged("AdminComment");
        OnAdminCommentChanged();
    }
}
private global::System.String _AdminComment;
partial void OnAdminCommentChanging(global::System.String value);
partial void OnAdminCommentChanged();

[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
[DataMemberAttribute()]
public global::System.Int32 ProductTemplateId
{
    get
    {
        return _ProductTemplateId;
    }
    set
    {
        OnProductTemplateIdChanging(value);
        ReportPropertyChanging("ProductTemplateId");
        _ProductTemplateId = StructuralObject.SetValidValue(value);
        ReportPropertyChanged("ProductTemplateId");
        OnProductTemplateIdChanged();
    }
}
private global::System.Int32 _ProductTemplateId;
partial void OnProductTemplateIdChanging(global::System.Int32 value);
partial void OnProductTemplateIdChanged();

[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
[DataMemberAttribute()]
public global::System.Boolean ShowOnHomePage
{
    get
    {
        return _ShowOnHomePage;
    }
    set
    {
        OnShowOnHomePageChanging(value);
        ReportPropertyChanging("ShowOnHomePage");
        _ShowOnHomePage = StructuralObject.SetValidValue(value);
        ReportPropertyChanged("ShowOnHomePage");
        OnShowOnHomePageChanged();
    }
}
private global::System.Boolean _ShowOnHomePage;
partial void OnShowOnHomePageChanging(global::System.Boolean value);
partial void OnShowOnHomePageChanged();

[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
[DataMemberAttribute()]
public global::System.String MetaKeywords
{
    get
    {
        return _MetaKeywords;
    }
    set
    {
        OnMetaKeywordsChanging(value);
        ReportPropertyChanging("MetaKeywords");
        _MetaKeywords = StructuralObject.SetValidValue(value, true);
        ReportPropertyChanged("MetaKeywords");
        OnMetaKeywordsChanged();
    }
}
private global::System.String _MetaKeywords;
partial void OnMetaKeywordsChanging(global::System.String value);
partial void OnMetaKeywordsChanged();

[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
[DataMemberAttribute()]
public global::System.String MetaDescription
{
    get
    {
        return _MetaDescription;
    }
    set
    {
        OnMetaDescriptionChanging(value);
        ReportPropertyChanging("MetaDescription");
        _MetaDescription = StructuralObject.SetValidValue(value, true);
        ReportPropertyChanged("MetaDescription");
        OnMetaDescriptionChanged();
    }
}
private global::System.String _MetaDescription;
partial void OnMetaDescriptionChanging(global::System.String value);
partial void OnMetaDescriptionChanged();

[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
[DataMemberAttribute()]
public global::System.String MetaTitle
{
    get
    {
        return _MetaTitle;
    }
    set
    {
        OnMetaTitleChanging(value);
        ReportPropertyChanging("MetaTitle");
        _MetaTitle = StructuralObject.SetValidValue(value, true);
        ReportPropertyChanged("MetaTitle");
        OnMetaTitleChanged();
    }
}
private global::System.String _MetaTitle;
partial void OnMetaTitleChanging(global::System.String value);
partial void OnMetaTitleChanged();

[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
[DataMemberAttribute()]
public global::System.String SeName
{
    get
    {
        return _SeName;
    }
    set
    {
        OnSeNameChanging(value);
        ReportPropertyChanging("SeName");
        _SeName = StructuralObject.SetValidValue(value, true);
        ReportPropertyChanged("SeName");
        OnSeNameChanged();
    }
}
private global::System.String _SeName;
partial void OnSeNameChanging(global::System.String value);
partial void OnSeNameChanged();

[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
[DataMemberAttribute()]
public global::System.Boolean AllowCustomerReviews
{
    get
    {
        return _AllowCustomerReviews;
    }
    set
    {
        OnAllowCustomerReviewsChanging(value);
        ReportPropertyChanging("AllowCustomerReviews");
        _AllowCustomerReviews = StructuralObject.SetValidValue(value);
        ReportPropertyChanged("AllowCustomerReviews");
        OnAllowCustomerReviewsChanged();
    }
}
private global::System.Boolean _AllowCustomerReviews;
partial void OnAllowCustomerReviewsChanging(global::System.Boolean value);
partial void OnAllowCustomerReviewsChanged();

[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
[DataMemberAttribute()]
public global::System.Int32 ApprovedRatingSum
{
    get
    {
        return _ApprovedRatingSum;
    }
    set
    {
        OnApprovedRatingSumChanging(value);
        ReportPropertyChanging("ApprovedRatingSum");
        _ApprovedRatingSum = StructuralObject.SetValidValue(value);
        ReportPropertyChanged("ApprovedRatingSum");
        OnApprovedRatingSumChanged();
    }
}
private global::System.Int32 _ApprovedRatingSum;
partial void OnApprovedRatingSumChanging(global::System.Int32 value);
partial void OnApprovedRatingSumChanged();

[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
[DataMemberAttribute()]
public global::System.Int32 NotApprovedRatingSum
{
    get
    {
        return _NotApprovedRatingSum;
    }
    set
    {
        OnNotApprovedRatingSumChanging(value);
        ReportPropertyChanging("NotApprovedRatingSum");
        _NotApprovedRatingSum = StructuralObject.SetValidValue(value);
        ReportPropertyChanged("NotApprovedRatingSum");
        OnNotApprovedRatingSumChanged();
    }
}
private global::System.Int32 _NotApprovedRatingSum;
partial void OnNotApprovedRatingSumChanging(global::System.Int32 value);
partial void OnNotApprovedRatingSumChanged();

[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
[DataMemberAttribute()]
public global::System.Int32 ApprovedTotalReviews
{
    get
    {
        return _ApprovedTotalReviews;
    }
    set
    {
        OnApprovedTotalReviewsChanging(value);
        ReportPropertyChanging("ApprovedTotalReviews");
        _ApprovedTotalReviews = StructuralObject.SetValidValue(value);
        ReportPropertyChanged("ApprovedTotalReviews");
        OnApprovedTotalReviewsChanged();
    }
}
private global::System.Int32 _ApprovedTotalReviews;
partial void OnApprovedTotalReviewsChanging(global::System.Int32 value);
partial void OnApprovedTotalReviewsChanged();

[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
[DataMemberAttribute()]
public global::System.Int32 NotApprovedTotalReviews
{
    get
    {
        return _NotApprovedTotalReviews;
    }
    set
    {
        OnNotApprovedTotalReviewsChanging(value);
        ReportPropertyChanging("NotApprovedTotalReviews");
        _NotApprovedTotalReviews = StructuralObject.SetValidValue(value);
        ReportPropertyChanged("NotApprovedTotalReviews");
        OnNotApprovedTotalReviewsChanged();
    }
}
private global::System.Int32 _NotApprovedTotalReviews;
partial void OnNotApprovedTotalReviewsChanging(global::System.Int32 value);
partial void OnNotApprovedTotalReviewsChanged();

[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
[DataMemberAttribute()]
public global::System.Boolean Published
{
    get
    {
        return _Published;
    }
    set
    {
        OnPublishedChanging(value);
        ReportPropertyChanging("Published");
        _Published = StructuralObject.SetValidValue(value);
        ReportPropertyChanged("Published");
        OnPublishedChanged();
    }
}
private global::System.Boolean _Published;
partial void OnPublishedChanging(global::System.Boolean value);
partial void OnPublishedChanged();

[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
[DataMemberAttribute()]
public global::System.Boolean Deleted
{
    get
    {
        return _Deleted;
    }
    set
    {
        OnDeletedChanging(value);
        ReportPropertyChanging("Deleted");
        _Deleted = StructuralObject.SetValidValue(value);
        ReportPropertyChanged("Deleted");
        OnDeletedChanged();
    }
}
private global::System.Boolean _Deleted;
partial void OnDeletedChanging(global::System.Boolean value);
partial void OnDeletedChanged();

[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
[DataMemberAttribute()]
public Nullable<global::System.DateTime> CreatedOnUtc
{
    get
    {
        return _CreatedOnUtc;
    }
    set
    {
        OnCreatedOnUtcChanging(value);
        ReportPropertyChanging("CreatedOnUtc");
        _CreatedOnUtc = StructuralObject.SetValidValue(value);
        ReportPropertyChanged("CreatedOnUtc");
        OnCreatedOnUtcChanged();
    }
}
private Nullable<global::System.DateTime> _CreatedOnUtc;
partial void OnCreatedOnUtcChanging(Nullable<global::System.DateTime> value);
partial void OnCreatedOnUtcChanged();

[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
[DataMemberAttribute()]
public Nullable<global::System.DateTime> UpdatedOnUtc
{
    get
    {
        return _UpdatedOnUtc;
    }
    set
    {
        OnUpdatedOnUtcChanging(value);
        ReportPropertyChanging("UpdatedOnUtc");
        _UpdatedOnUtc = StructuralObject.SetValidValue(value);
        ReportPropertyChanged("UpdatedOnUtc");
        OnUpdatedOnUtcChanged();
    }
}
private Nullable<global::System.DateTime> _UpdatedOnUtc;
partial void OnUpdatedOnUtcChanging(Nullable<global::System.DateTime> value);
partial void OnUpdatedOnUtcChanged();

[XmlIgnoreAttribute()]
[SoapIgnoreAttribute()]
[DataMemberAttribute()]
[EdmRelationshipNavigationPropertyAttribute("eSalesModel", "ProductCategory_Product", "Product_Category_Mapping")]
public EntityCollection<Product_Category_Mapping> Product_Category_Mapping
{
    get
    {
        return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection<Product_Category_Mapping>("eSalesModel.ProductCategory_Product", "Product_Category_Mapping");
    }
    set
    {
        if ((value != null))
        {
            ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedCollection<Product_Category_Mapping>("eSalesModel.ProductCategory_Product", "Product_Category_Mapping", value);
        }
    }
}

[XmlIgnoreAttribute()]
[SoapIgnoreAttribute()]
[DataMemberAttribute()]
[EdmRelationshipNavigationPropertyAttribute("eSalesModel", "ProductManufacturer_Product", "Product_Manufacturer_Mapping")]
public EntityCollection<Product_Manufacturer_Mapping> Product_Manufacturer_Mapping
{
    get
    {
        return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection<Product_Manufacturer_Mapping>("eSalesModel.ProductManufacturer_Product", "Product_Manufacturer_Mapping");
    }
    set
    {
        if ((value != null))
        {
            ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedCollection<Product_Manufacturer_Mapping>("eSalesModel.ProductManufacturer_Product", "Product_Manufacturer_Mapping", value);
        }
    }
}

[XmlIgnoreAttribute()]
[SoapIgnoreAttribute()]
[DataMemberAttribute()]
[EdmRelationshipNavigationPropertyAttribute("eSalesModel", "ProductPicture_Product", "Product_Picture_Mapping")]
public EntityCollection<Product_Picture_Mapping> Product_Picture_Mapping
{
    get
    {
        return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection<Product_Picture_Mapping>("eSalesModel.ProductPicture_Product", "Product_Picture_Mapping");
    }
    set
    {
        if ((value != null))
        {
            ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedCollection<Product_Picture_Mapping>("eSalesModel.ProductPicture_Product", "Product_Picture_Mapping", value);
        }
    }
}
[XmlIgnoreAttribute()]
[SoapIgnoreAttribute()]
[DataMemberAttribute()]
[EdmRelationshipNavigationPropertyAttribute("eSalesModel", "ProductReview_Product1", "ProductReview")]
public EntityCollection<ProductReview> ProductReviews
{
    get
    {
        return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection<ProductReview>("eSalesModel.ProductReview_Product1", "ProductReview");
    }
    set
    {
        if ((value != null))
        {
            ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedCollection<ProductReview>("eSalesModel.ProductReview_Product1", "ProductReview", value);
        }
    }
}
[XmlIgnoreAttribute()]
[SoapIgnoreAttribute()]
[DataMemberAttribute()]
[EdmRelationshipNavigationPropertyAttribute("eSalesModel", "ProductSpecificationAttribute_Product", "Product_SpecificationAttribute_Mapping")]
public EntityCollection<Product_SpecificationAttribute_Mapping> Product_SpecificationAttribute_Mapping
{
    get
    {
        return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection<Product_SpecificationAttribute_Mapping>("eSalesModel.ProductSpecificationAttribute_Product", "Product_SpecificationAttribute_Mapping");
    }
    set
    {
        if ((value != null))
        {
            ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedCollection<Product_SpecificationAttribute_Mapping>("eSalesModel.ProductSpecificationAttribute_Product", "Product_SpecificationAttribute_Mapping", value);
        }
    }
}
[XmlIgnoreAttribute()]
[SoapIgnoreAttribute()]
[DataMemberAttribute()]
[EdmRelationshipNavigationPropertyAttribute("eSalesModel", "ProductVariant_Product", "ProductVariant")]
public EntityCollection<ProductVariant> ProductVariants
{
    get
    {
        return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection<ProductVariant>("eSalesModel.ProductVariant_Product", "ProductVariant");
    }
    set
    {
        if ((value != null))
        {
            ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedCollection<ProductVariant>("eSalesModel.ProductVariant_Product", "ProductVariant", value);
        }
    }
}
[XmlIgnoreAttribute()]
[SoapIgnoreAttribute()]
[DataMemberAttribute()]
[EdmRelationshipNavigationPropertyAttribute("eSalesModel", "Product_ProductTag_Mapping", "ProductTag")]
public EntityCollection<ProductTag> ProductTags
{
    get
    {
        return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection<ProductTag>("eSalesModel.Product_ProductTag_Mapping", "ProductTag");
    }
    set
    {
        if ((value != null))
        {
            ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedCollection<ProductTag>("eSalesModel.Product_ProductTag_Mapping", "ProductTag", value);
        }
    }
}

}

4

1 回答 1

1

是的,我知道发生了什么......基本上,我在事务中运行这一切,并且在产品之前导出其他实体(类别)。问题是我在添加所有类别后没有打电话SaveChanges()......所以出现的错误是关于类别表而不是产品表,但我只有SaveChanges()在我添加后才收到消息产品也是如此..非常令人困惑..我希望将来他们会通过说明错误来自哪个表来更清楚地说明..这将节省大量时间。

这是我的情况,你看不到为什么我没有立即发现问题所在:

using (var transactionScope = new TransactionScope(TransactionScopeOption.Required, TimeSpan.FromMinutes(30)))
            using (var piloDB = new PiloEntities())
            using (var eSalesDB = new ESalesEntities())
            {
                ExportCategories(piloDB, eSalesDB); //SaveChanges() not called in here
                ExportProducts(piloDB, eSalesDB); //SaveChanges() is called in here
                //export other...
于 2012-08-08T03:06:25.007 回答