gpt4 book ai didi

c# - 该属性必须是有效的实体类型,并且该属性应该具有非抽象 getter 和 setter。 Entity Framework

转载 作者:行者123 更新时间:2023-12-01 22:50:30 27 4
gpt4 key购买 nike

我在 Entity Framework 中首先从代码创建数据库时遇到问题。我有以下三个类 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 类

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; }
}
}

个人就业历史

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 是有效的实体类型。

最佳答案

我也是先学习代码。我使用的是 Fluent API,所以我对注释没有那么丰富的经验。我认为你做外键的方式是错误的。我认为它们应该看起来像:

public int UserId { get; set; }
[ForeignKey("UserId")]
public virtual Person User{ get; set; }

不同之处在于您有一个 FK 类型的变量并对其进行注释。您还可以查找 Fluent API。

参见http://www.codeproject.com/Articles/319366/EF-Code-First-Add-a-Foreign-Key-relationship

关于c# - 该属性必须是有效的实体类型,并且该属性应该具有非抽象 getter 和 setter。 Entity Framework ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26559067/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com