c#-使用Code-First MVC5 EF6在SQL表中存储DateTime属性而不是字节数组
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c#-使用Code-First MVC5 EF6在SQL表中存储DateTime属性而不是字节数组,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3842字,纯文字阅读大概需要6分钟。
内容图文
当我尝试在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.
内容总结
以上是互联网集市为您收集整理的c#-使用Code-First MVC5 EF6在SQL表中存储DateTime属性而不是字节数组全部内容,希望文章能够帮你解决c#-使用Code-First MVC5 EF6在SQL表中存储DateTime属性而不是字节数组所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。