0

我有一个使用 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; }
4

1 回答 1

0

也许发生这种情况是因为您的用户表中的 Guid 没有被填充?尝试使用此 PK 属性:

 [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
于 2012-05-10T06:39:04.310 回答