标签:asp-net-mvc-5 entity-framework-6 datetime sql c
当我尝试在MVC5中使用EF6初始化数据库时,出现错误:属性“ Timestamp”不是字节数组.只能为字节数组属性配置IsRowVersion.有没有一种方法可以使用FluentAPI覆盖IsRowVersion,还是有另一种方法可以使用MVC5 EF6存储DateTime,或者这仅仅是使用Timestamp数据注释的结果?我更喜欢存储为DateTime而不是字节数组.只是为了可视化,模型看起来像这样:
public class UserProfile : IdentityUser
{
//ctor
public UserProfile()
{
Random rnd = new Random();
int pic = rnd.Next(1, 6);
DateJoined = DateTime.Now;
UserLevel = 4;
ImageUrl = "/Content/Avatars/Samples/sample" + pic + ".jpg"; // or append .jpg which == .jpg
}
public UserProfile(string name, string url)
{
UserName = name;
DateJoined = DateTime.Now;
UserLevel = 4;
ImageUrl = url;
Email = "your@email.com";
}
public UserProfile(string name, string url, string email)
{
Random rnd = new Random();
int pic = rnd.Next(1, 6);
UserName = name;
DateJoined = DateTime.Now;
UserLevel = 4;
ImageUrl = "/Content/Avatars/sample" + pic + ".jpg"; // or append .jpg which == .jpg
Email = email;
}
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
[Required(ErrorMessage="*Username is required."), RegularExpression(@"^[\p{L} \p{Nd}_]+$")]
[MinLengthAttribute(5, ErrorMessage="Not enough characters! UserName must be at least 5 chars."), MaxLengthAttribute(30,ErrorMessage="Too many characters in UserName!")]
public override string UserName { get; set; }
[Required(ErrorMessage="*Email is required."), EmailAddress, RegularExpression(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*")]
[DataType(DataType.EmailAddress)]
[Display(Name = "Email address")]
public string Email { get; set; }
[ScaffoldColumn(false),Required]
public string ImageUrl { get; set; }
[DisplayFormat(DataFormatString = "{0}")] //{0:d} or {0:D}
[DataType(DataType.DateTime), Timestamp, ScaffoldColumn(false)] //<--Problem
public DateTime DateJoined { get; set; }
[Range(1, 6), ScaffoldColumn(false)]
public int UserLevel { get; set; }
[ScaffoldColumn(false)]
public int? TotalRepPoints { get; set; }
[ScaffoldColumn(false)]
public virtual IDictionary<int, int> TotalPointsByCat { get; set; }
// int = CategoryId, int = UserRank
public virtual IDictionary<int, int> Rankings { get; set; }
public virtual ICollection<Article> Articles { get; set; }
public virtual ICollection<Answer> Answers { get; set; }
public virtual ICollection<Vote> Votes { get; set; }
public virtual ICollection<Question> Questions { get; set; }
}
当我使用NuGet控制台时:
PM> Enable-Migrations -Force
Checking if the context targets an existing database...
System.InvalidOperationException: The property 'Timestamp' is not a Byte array. IsRowVersion can only be configured for Byte array properties.
at System.Data.Entity.ModelConfiguration.Configuration.ConventionPrimitivePropertyConfiguration.IsRowVersion()
at System.Data.Entity.ModelConfiguration.Conventions.TimestampAttributeConvention.Apply(ConventionPrimitivePropertyConfiguration configuration, TimestampAttribute attribute)
at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
...
The property 'Timestamp' is not a Byte array. IsRowVersion can only be configured for Byte array properties.
PM>
提前致谢!
编辑
使用数据注释存储属性时[DataType(DataType.DateTime)]
与DateTime?类,您将在SQL中获得datetime类型字段.
使用数据注释存储属性时[时间戳记]
使用byte [],您将获得SQL中的时间戳记类型字段.
解决方法:
从Microsoft documentation on timestamp
(AKA rowversion
)(添加重点)开始:
Is a data type that exposes automatically generated, unique binary numbers within a database. rowversion is generally used as a mechanism for version-stamping table rows. The storage size is 8 bytes. The rowversion data type is just an incrementing number and does not preserve a date or a time. To record a date or time, use a datetime2 data type.
因此,尽管这个数据类型起了旧名称,但实际上与我们通常认为的实际日期时间值无关.
选择其他列类型,例如datetime.
标签:asp-net-mvc-5,entity-framework-6,datetime,sql,c 来源: https://codeday.me/bug/20191029/1962808.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。