在 EF 5 中定义导航 属性,代码优先迁移
Defining a Navigation property in EF 5, Code first migrations
查看我的模型
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; }
}
我想要的是在文件 table 中为用户 ID 定义一个外键。我应该怎么做。我尝试使用 [ForeignKey]
属性。但是它在 PCM 中执行 update-database -verbose
时给了我这个错误。
Error - The navigation property 'UserId' is not a declared property on
type 'Files'. Verify that it has not been explicitly excluded from the
model and that it is a valid navigation property.
[Table("Files")]
public class Files
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
[Display(Name = "FileID")]
public int FileID { get; set; }
[Required]
[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; }
[ForeignKey("UserId")] // this is what I have tried
public virtual UserProfile UserProfile { get; set; }
}
查看我的模型
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; }
}
我想要的是在文件 table 中为用户 ID 定义一个外键。我应该怎么做。我尝试使用 [ForeignKey]
属性。但是它在 PCM 中执行 update-database -verbose
时给了我这个错误。
Error - The navigation property 'UserId' is not a declared property on type 'Files'. Verify that it has not been explicitly excluded from the model and that it is a valid navigation property.
[Table("Files")]
public class Files
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
[Display(Name = "FileID")]
public int FileID { get; set; }
[Required]
[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; }
[ForeignKey("UserId")] // this is what I have tried
public virtual UserProfile UserProfile { get; set; }
}