当我尝试在MVC5中使用EF6初始化我的数据库时,我收到错误:属性'Timestamp'不是Byte数组。 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类型字段。
使用数据注释[Timestamp]
存储属性时
使用byte[]
,您将在SQL中获得时间戳类型字段。
答案 0 :(得分:4)
从Microsoft documentation on timestamp
(AKA rowversion
)(强调添加):
是一种在数据库中公开自动生成的唯一二进制数的数据类型。 rowversion通常用作版本标记表行的机制。存储大小为8个字节。 rowversion数据类型只是一个递增的数字,不会保留日期或时间。要记录日期或时间,请使用datetime2数据类型。
所以这个数据类型,尽管它的旧名称,基本上与我们通常认为的实际日期时间值无关。
选择其他列类型,例如datetime。