实体MVC3使用GUID播种数据

时间:2012-05-10 01:57:12

标签: asp.net-mvc

我有一个使用GUID类型作为外键的用户类。我想将数据播种到另一个使用来自用户类的GUID的表,但得到“无法显式地将字符串转换为system.GUID”错误。

这是我想要播种的表格:

public class Song
    {
        [ScaffoldColumn(false)]
        public int Songid { get; set; }

        [ScaffoldColumn(false)]
        public virtual Guid UserId { get; set; }

        public string SongTitle { get; set; }

        public string Genre { get; set; }

        public string insWanted { get; set; }

        public string mp3URL { get; set; }

        public string wavURL { get; set; }

        public int? BPM { get; set; }
    }

以下是用户类:

public class User
    {
        [Key]
        public virtual Guid UserId { get; set; }

        [Required]
        public virtual String Username { get; set; }

        [Required]
        public virtual String Email { get; set; }

        [Required, DataType(DataType.Password)]
        public virtual String Password { get; set; }

        public virtual String FirstName { get; set; }
        public virtual String LastName { get; set; }

        [DataType(DataType.MultilineText)]
        public virtual String Comment { get; set; }

        public virtual Boolean IsApproved { get; set; }
        public virtual int PasswordFailuresSinceLastSuccess { get; set; }
        public virtual DateTime? LastPasswordFailureDate { get; set; }
        public virtual DateTime? LastActivityDate { get; set; }
        public virtual DateTime? LastLockoutDate { get; set; }
        public virtual DateTime? LastLoginDate { get; set; }
        public virtual String ConfirmationToken { get; set; }
        public virtual DateTime? CreateDate { get; set; }
        public virtual Boolean IsLockedOut { get; set; }
        public virtual DateTime? LastPasswordChangedDate { get; set; }
        public virtual String PasswordVerificationToken { get; set; }
        public virtual DateTime? PasswordVerificationTokenExpirationDate { get; set; }

        public virtual ICollection<Role> Roles { get; set; }

1 个答案:

答案 0 :(得分:0)

也许这是因为你的Guid in User表没有被填充?尝试使用这个PK属性:

 [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]