我想将“UserProfile”与另一个模型“Data”联系起来。
UserProfile模型:
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public string FullName { get; set; }
}
数据模型:
[Table("Data")]
public class Data
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int DataID { get; set; }
[Required]
[Display(Name = "DataName")]
public string DataName { get; set; }
[Required]
[Display(Name = "Created Date")]
public DateTime CreatedDate { get; set; }
public int? ManagedByUserId { get; set; }
[Required]
[ForeignKey("ManagedByUserId")]
[Display(Name = "Managed By")]
public UserProfile ManagedByUser { get; set; }
}
这里ManagedByUserId是Data表中的foriegnkey。
Namming会议有什么问题吗?但是我收到了属性错误"ManagedByUser" is required
。因此,我的ModelState
无效(ModelState.IsValid
为false
),因此不会保存数据。