1
见我的模型在EF 5定义导航属性,代码首先迁移
public class UsersContext : DbContext
{
public UsersContext()
: base("DefaultConnection")
{
}
public DbSet<UserProfile> UserProfiles { get; set; }
public DbSet<Files> Files { get; set; }
}
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
}
[Table("Files")]
public class Files
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
[Display(Name = "FileID")]
public int FileID { get; set; }
[Required]
[ForeignKey("UserId")] // this is what I have tried
[Display(Name = "For User")]
public int UserId { get; set; }
[Display(Name = "Description")]
public string Desc { get; set; }
[Required]
[Display(Name = "Document Upload")]
public string DocumentPath { get; set; }
}
我想是定义在文件表中的用户ID的外键。我该怎么做。我尝试使用[ForeignKey]
属性。但它在PCM中执行update-database -verbose
时给我这个错误。
错误 - 导航属性'UserId'不是 类型'文件'的声明属性。验证它是否未被明确地从 模型中排除,并且它是有效的导航属性。
1.你能告诉你尝试过的代码? 2.你有什么错误? 3.如果你已经有数据并添加一个不可为空的关系,你当然应该更新你的数据... –
@RaphaëlAlthaus请看我的编辑.. –