我收到此错误:“实体类型'DisplayFormatAttribute'需要定义主键。”

时间:2019-06-12 01:41:31

标签: c# entity-framework ef-code-first entity-framework-core

我收到此错误

“实体类型'DisplayFormatAttribute'需要定义主键。”在终端上,当我尝试运行此代码时

Dotnet ef migrations add firstMigrationAddModels

我正在使用Entity-framework core 2.0创建代码优先的数据库迁移

我有许多模型(类),一个类是从另一个类继承的,为了解决该问题,我使用了称为“逐层表(TPH)”的Entity Framework核心的继承功能

https://www.learnentityframeworkcore.com/inheritance

我将派生类和基类都放在dbcontext的Dbset中

public DbSet<Person> people { get; set; }
public DbSet<Student> students { get; set; }

STUDENT类没有PK,因为Person有它。

我也有具有很多对许多关系的类,我通过创建一个桥类来解决了

错误表明我需要“ DisplayFormatAttribute”中的主键 但我没有该课程的权限

我在模型中使用了诸如Maxlength()和minlength()之类的DataAnotationAttributes,所以我以某种方式访问​​该类。

我正在使用的其他类型是PhoneAttribute,EmailAddressAttribute

[MaxLength(15)]
public PhoneAttribute Phone { get; set; }
[MaxLength(254)]
public EmailAddressAttribute Email { get; set; }

3 个答案:

答案 0 :(得分:1)

您必须使用数据注释[Key]

设置属性之一

[Key]实际上将是您数据表的标识符。

答案 1 :(得分:0)

我解决了。问题是我的电话号码和电子邮件类型属性

public PhoneAttribute Phone { get; set; }
public EmailAddressAttribute Email { get; set; }

PhoneAttribute和EmailAddressAttributes没有SQL Server等效类型

我将它们取出,分别将它们更改为int和string,并且有效

public int Phone { get; set; }
public string Email { get; set; }

答案 2 :(得分:0)

我解决了。问题是我的电话号码和电子邮件类型属性

public PhoneAttribute Phone { get;放; } 公共 EmailAddressAttribute 电子邮件 { 获取;放; } PhoneAttribute 和 EmailAddressAttributes 没有 SQL 服务器等效类型

我把它们取出来,分别改成 int 和 string 就行了

public int Phone { get;放; } 公共字符串电子邮件{获取;放; }

Т他对我的作品非常感谢