我在实体框架中首先从代码创建数据库时遇到问题。我有以下三个类1 Person,2 PersonAddress,3 PersonEmploymentHistory,如下所示。
namespace DataAccess.Models
{
[Table("Profile")]
public class Person
{
[Key]
public int UserId { get; set; }
[Required(ErrorMessage = "Firstname is required")]
public string PersonFirstName { get; set; }
[Required(ErrorMessage = "Surname is required")]
public string PersonSurname { get; set; }
[Required(ErrorMessage = "Email is required")]
[DataType(DataType.EmailAddress)]
public string PersonEmail { get; set; }
public int Age { get; set; }
[Required(ErrorMessage = "Contact Number required")]
public Int64 PersonNumber { get; set; }
public bool IsActive { get; set; }
[ForeignKey("AddressDetails")]
public int AddressId { get; set; }
public virtual PersonAddress AddressDetails { get; set; }
[ForeignKey("EmploymentHistory")]
public int EmployerId { get; set; }
public virtual PersonEmploymentHistory EmploymentHistory { get; set; }
}
}
PersonAddress Class
namespace DataAccess.Models
{
[Table("AddressDetails")]
public class PersonAddress
{
[Key]
public int AddressId { get; set; }
[ForeignKey("UserId")]
public int UserId { get; set; }
[Required(ErrorMessage = "Address Line 1 required")]
public string AddressLine1 { get; set; }
[Required(ErrorMessage = "Address Line 2 required")]
public string AddressLine2 { get; set; }
[Required(ErrorMessage = "Postcode required")]
[DataType(DataType.PostalCode)]
public string PostCode { get; set; }
}
}
PersonEmploymentHistory
namespace DataAccess.Models
{
[Table("EmploymentHistory")]
public class PersonEmploymentHistory
{
[Key]
public int EmployerId { get; set; }
public string EmployerName { get; set; }
public decimal EmployeeSalary { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public string ReasonForLeaving { get; set; }
[ForeignKey("UserId")]
public int UserId { get; set; }
}
}
这是我的Context类
public class Context : DbContext
{
public Context()
: base("DefaultConnection")
{
Database.SetInitializer<Context>(new CreateDatabaseIfNotExists<Context>());
}
public DbSet<Person> Person { get; set; }
public DbSet<PersonAddress> PersonAddress { get; set; }
public DbSet<PersonEmploymentHistory> PersonEmployment { get; set; }
}
现在我在创建外键等时已经遵循了教程,但是当我运行我的项目并尝试将数据插入到人员表中时,我收到以下错误
无法将属性“UserId”配置为导航属性。该属性必须是有效的实体类型,并且该属性应具有非抽象的getter和setter。对于集合属性,类型必须实现ICollection,其中T是有效的实体类型。
答案 0 :(得分:12)
我也是先学习代码。我使用的是流畅的API,所以我对注释没有经验。我认为你做外键的方式是错误的。我认为他们应该是这样的:
public int UserId { get; set; }
[ForeignKey("UserId")]
public virtual Person User{ get; set; }
区别在于你有一个FK类型的变量并注释它。您还可以查找流畅的API。
请参阅http://www.codeproject.com/Articles/319366/EF-Code-First-Add-a-Foreign-Key-relationship
答案 1 :(得分:1)
在开发和关联多个表时,我遇到了同样的问题,如果使用 FirstCode ,我建议使用以下结构:
在“ PersonAddress”类中
public long UserId{ get; set; }
[ForeignKey("UserId")]
public Person Person { get; set; }
对于与您相关的表,使用外键指向对象或对象下方。
不常用:
[ForeignKey("UserId")]
public int UserId { get; set; }
您将对要关联的其他表执行相同的操作。
答案 2 :(得分:-1)
在ForeignKey中,您需要添加类引用。更改为:
[ForeignKey("Person")]
public int UserId { get; set; }
public virtual Person User{ get; set; }
答案 3 :(得分:-1)
[ForeignKey("UserId")]
public virtual Person User{ get; set; }
[ForeignKey("User")]
public int UserId { get; set; }
你应该像这样使用它。我也有同样的问题,我已经解决了这个问题。