Robert Martin
Robert Martin

Reputation: 17177

How can I make a Guid primary key using MVC Code First Entities?

I am trying to make a one-to-one relationship between the aspnet_Users table and my own entity, Player. To accomplish this, I added a Guid:

public class Player
    {
        public Guid ID;
        // ...

However, this isn't automatically becoming the primary key, as I intended. How can I coerce this column to become the primary key?

Update: After looking here, I added this to my Player class:

using System.ComponentModel.DataAnnotations;
public class Player
    {
        [Key,Required]
        public Guid ID;
        // ...

But still, when I try to do a @foreach in my View, I get the error " EntityType 'Player' has no key defined"

Upvotes: 2

Views: 1602

Answers (1)

Andy
Andy

Reputation: 1630

You need {get; set;} to make it a property.

Upvotes: 4

Related Questions