我正在尝试将“UserProfile”与另一个模型“Data”相关联。
用户档案模型:
[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 表中的外键。
命名约定有什么问题吗?但是我收到一个属性错误"ManagedByUser" is required
。因此, myModelState
无效(ModelState.IsValid
is false
),因此不保存数据。