Saturday 15 August 2015

c# - Foreign key with Entity Framework -


I have two model classes User and Reclamation, a foreign key for the user object in the recession class, here my two classes I:

  public class user {{key, scaffold column (wrong), display (name = "id user")] public antioxidian user {set; get; } [Required, string lang (16), display (name = "login")] public string login {set; get; } [Required, string lang (16), display (name = "password")] public string password {set; get; }} Public Class Reclamation {{Key, Scaffold Column (false), Display (name = "id reclamation")} Public Ent ID idation {set; get; } [Foreign user ("iduser")] public user user {set; get; } [Required, Display (name = "id protector")] Public encryption user {set; get; } [Required, string lamps (30), display (name = "message")] public string message {set; get; }}  

For example, say I have a user with IDUuser = 1 when I create a new reclamation with idUser = 1 and then the database will be inserted, but the user with the object In the problem the user's information should not be included with idUser = 1, should I manually code the set property in the rewlamation class IDUser attribute or am I missing something?

You can add virtual properties to enable lazy loading in your unit class. In this way you will be able to load the related object when EF is requested. Public class user {{key, scaffold column (wrong), display (name = "id user")} public int idUser {set; get; } [Required, string lang (16), display (name = "login")] public string login {set; get; } [Required, string lang (16), display (name = "password")] public string password {set; get; } Public Virtual Reclamation Reaction {get; Set;} / * If you have many relationships, then use the collection & lt; T & gt; And this is the constructor reslation = start in new collection & lt; Reclamation & gt; (); * / Public virtual archive & lt; Reclamation & gt; Retrieve {Receive; Set;}}

Your second option is to use curious loading as mentioned above. Something like this:

  context. the user. Included (x = & gt; x. Recapitulation). Ollist ();  

No comments:

Post a Comment