filmov
tv
[SOLVED] - The entity type requires a primary key to be defined
Показать описание
This video is about to Fix The entity type requires a primary key to be defined. If you intended to use a keyless entity type, call 'HasNoKey' in 'OnModelCreating'. For more information on keyless entity types
Solution:
This exception message doesn't mean it requires a primary key to be defined in your database, it means it requires a primary key to be defined in your class.
Although you've attempted to do so:
private Guid _id;
[Key]
public Guid ID
{
get { return _id; }
}
This has no effect, as Entity Framework ignores read-only properties. It has to: when it retrieves a Fruits record from the database, it constructs a Fruit object, and then calls the property setters for each mapped property. That's never going to work for read-only properties.
You need Entity Framework to be able to set the value of ID. This means the property needs to have a setter.
Solution:
This exception message doesn't mean it requires a primary key to be defined in your database, it means it requires a primary key to be defined in your class.
Although you've attempted to do so:
private Guid _id;
[Key]
public Guid ID
{
get { return _id; }
}
This has no effect, as Entity Framework ignores read-only properties. It has to: when it retrieves a Fruits record from the database, it constructs a Fruit object, and then calls the property setters for each mapped property. That's never going to work for read-only properties.
You need Entity Framework to be able to set the value of ID. This means the property needs to have a setter.