0
votes

I am getting this error

"The entity type 'DisplayFormatAttribute' requires a primary key to be defined." on the terminal when I try to run this code

Dotnet ef migrations add firstMigrationAddModels

I am creating a code first database migration using Entity-framework core 2.0

I have many models (Classes) and One class inherits from another, To solve that problem I used the Inheritance functionality of the Entity Framework core called Table Per Hierarchy (TPH)

https://www.learnentityframeworkcore.com/inheritance

I put both the derived and base class in Dbset of the dbcontext

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

The STUDENT class doesn't have PK because the Person has it.

I also have classes that have many to many relationships and I solved that by creating a bridge class

The error says I need the primary key inside "DisplayFormatAttribute" but I don't have access to that class

I am using DataAnotationAttributes like Maxlength() and minlength() in my models so I am accessing that class some how.

Other types I am using are PhoneAttribute,EmailAddressAttribute

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

3 Answers

1
votes

I solved it. The problem was my phone number and email type attributes

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

There is no SQL server equivalent type for PhoneAttribute and EmailAddressAttributes

I took them out and changed them to int and string respectively and it worked

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

You have to set one of your properties using the data annotation [Key]

This [Key] will practically be the identifier of your data table.

0
votes

I solved it. The problem was my phone number and email type attributes

public PhoneAttribute Phone { get; set; } public EmailAddressAttribute Email { get; set; } There is no SQL server equivalent type for PhoneAttribute and EmailAddressAttributes

I took them out and changed them to int and string respectively and it worked

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

Đ¢his works for me thank you very much