Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Asp.net MVC 5数据库初始值设定项未基于日期时间问题进行种子设定_C#_Asp.net Mvc_Datetime_Asp.net Mvc 5_Ef Code First - Fatal编程技术网

C# Asp.net MVC 5数据库初始值设定项未基于日期时间问题进行种子设定

C# Asp.net MVC 5数据库初始值设定项未基于日期时间问题进行种子设定,c#,asp.net-mvc,datetime,asp.net-mvc-5,ef-code-first,C#,Asp.net Mvc,Datetime,Asp.net Mvc 5,Ef Code First,请接受任何帮助,我们将不胜感激 奇怪的是,结果被记录在数据库中,我使用代码优先的方法,下面是一个模型示例: using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace STRA.Models { public class Category { public int Id { get; set; } public str

请接受任何帮助,我们将不胜感激

奇怪的是,结果被记录在数据库中,我使用代码优先的方法,下面是一个模型示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace STRA.Models
{
    public class Category
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string CreatedBy { get; set; }
        //public Guid CreatedBy { get; set; }
        //var createdByEmail = User.Identity.GetUserName();
        //var createdById = User.Identity.GetUserId();
        public DateTime DateCreated { get; set; }
        public DateTime DateModified { get; set; }
        public virtual ICollection<Question> Question { get; set; }
        public virtual ICollection<CategoryFeedback> CategoryFeedback { get; set; }
        public virtual ICollection<UserCategoryResult> UserCategoryResult { get; set; }
    }
}

Models\ApplicationBinInitializer.cs行:391

此问题的根本原因是SQL server和C#中日期时间类型的不同范围。C#Datetime的最小值为1/1/0001 12:00:00 AM。如果SQL server中的字段数据类型为datetime,它将无法存储此值,因为SQL server中支持的最小范围为01/01/1753 00:00:00

在sql server中将其更改为datetime2,问题将得到解决

我猜,在您的情况下,asp.net以某种方式发送了min.value,可能是因为您尚未为某些属性设置日期时间

谢谢大家! 好的,问题解决了!我发现有几种方法可以解决这个问题,包括使数据类型为null,以及在每个类中创建多态方法:

using IdentitySample.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace STRA.Models
{
    public class UserQuestionResult
    {
        public UserQuestionResult()
        {
            DateCreated = DateTime.Now;
            DateModified = DateTime.Now;
        }
        public int Id { get; set; }
        public DateTime? DateCreated { get; set; }
        public DateTime? DateModified { get; set; }
        public int UserRating { get; set; }
        //navigation properties
        public virtual ApplicationUser User { get; set; }
        //public ICollection<ApplicationUser> ApplicationUser { get; set; }
        //public Guid ApplicationUserId { get; set; }
        //public ICollection<Report> Report { get; set; }
        public virtual Question Question { get; set; }
        public int QuestionId { get; set; }
    }
}
使用IdentitySample.Models;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
命名空间STRA.Models
{
公共类UserQuestionResult
{
公共用户提问结果()
{
DateCreated=DateTime.Now;
DateModified=DateTime.Now;
}
公共int Id{get;set;}
公共日期时间?DateCreated{get;set;}
公共日期时间?日期修改{get;set;}
public int UserRating{get;set;}
//导航属性
公共虚拟应用程序用户{get;set;}
//公共ICollection应用程序用户{get;set;}
//公共Guid ApplicationUserId{get;set;}
//公共ICollection报表{get;set;}
公共虚拟问题{get;set;}
public int QuestionId{get;set;}
}
}

您是否尝试过设置断点并检查实际尝试添加的日期时间值?是否确实为所有日期时间字段设置了日期时间值。您的初始值设定项中有许多实体没有设置日期时间值,但并没有显示所有的类。因为您没有将其设置为null,所以asp.net试图保存C#DateTime 01/01/0001 00:00:00的最小值,正如我在回答中所说的
Line 389:            # endregion
Line 390:
Line 391:            db.SaveChanges();
Line 392:        }
Line 393:    }
using IdentitySample.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace STRA.Models
{
    public class UserQuestionResult
    {
        public UserQuestionResult()
        {
            DateCreated = DateTime.Now;
            DateModified = DateTime.Now;
        }
        public int Id { get; set; }
        public DateTime? DateCreated { get; set; }
        public DateTime? DateModified { get; set; }
        public int UserRating { get; set; }
        //navigation properties
        public virtual ApplicationUser User { get; set; }
        //public ICollection<ApplicationUser> ApplicationUser { get; set; }
        //public Guid ApplicationUserId { get; set; }
        //public ICollection<Report> Report { get; set; }
        public virtual Question Question { get; set; }
        public int QuestionId { get; set; }
    }
}