c# - EF code first Reference key as primary key -


landed in quite ridiculous situation here.

i'm coding in ef core, asp.net core, visual studio 2017 community.

i have model not have primary key like:

public class loginrecord {     public user user { get; set; }      public int userid { get; set; }      public datetime lastlogin { get; set; }     public int logincount { get; set; }  } 

where user table fields name, email etc.

when add-migration, error pops saying:

the entity type 'loginrecord' requires primary key defined.

then tried adding [key] annotation userid field this:

public class loginrecord {     public user user { get; set; }      [key]         public int userid { get; set; }      public datetime lastlogin { get; set; }     public int logincount { get; set; }  } 

but now, ef creates new column called userid1 becomes foreign key.

i need userid reference key , don't need primary key table.

is possible? if yes please help!

you should add primary key. i've learned every table should field named table + id. name id if more. :) every record gets unique.

so advice change model this:

public class loginrecord {   [key]   public int loginrecordid {get; set;}    public user user { get; set; }    public int userid { get; set; }    public datetime lastlogin { get; set; }   public int logincount { get; set; }  } 

Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -