Entity Framework Code-First - Defining Relationships with MembershipUser

I am trying to figure out how best to define POCO classes in order to be able to use the first Entity Framework function.
I want to define some foreign key relationships in my classes, between the user, and also between the classes themselves. For example, consider the following 3 classes:

Public class Job { public int JobID {get; set;} public string JobTitle {get; set;} public virtual ICollection<Resume> Resumes {get; set;} // Is this correct at all? How to access all resumes for a certain job? (many-to-many relationship between Job and Employee) } Public class Resume { public int EmployeeID {get; set;} // or should it be: public virtual Employee EmployeePerson? public int JobID {get; set;} // or should it be: public virtual Job UserJob? public DateTime EmploymentDate {get; set;} } public class Employee { public int EmployeeID {get; set;} public int UserID{ger; set;} // or should it be: public virtual MembershipUser User? public ICollection<Resume> Resumes {get; set;} // Is this correct at all? } 

The user is a membership user located in System.Web.Security , which is authenticated using FormsAuthentication or ActiveDirectoryAuthentication. Questions are mentioned in the code (in the comments). But to clarify:

  • Should I define objects in relationships and use .Include every time I need them, or is it better to store the identifier of objects and try to get data from this identifier every time I need? Should I use a different approach when working with the MembershipUser class instead of my specific classes?
  • What other uses of virtual besides the possibility of lazy loading? Where should I avoid it and where to use it?

Thanks.

UPDATE . I just checked the definition of Employee with the definition of ublic virtual MembershipUser User . The result was 4 added columns in my table:

  • User_Email,
  • User_Comment,
  • User_IsApproved,
  • User_LastLoginDate,
  • User_LastActivityDate

Nothing unique to the user (User_Email is defined as NULL). So, if you want to have users in your classes, write a wrapper for MembershipUser or just save the UserID.
Thank you, Ladislav and Sergi.

+6
source share
2 answers

I would recommend declaring foreign keys as navigation properties, so you can always directly access the related property without loading it directly from the database (this will be lazily loaded).

So your model will look like this:

 public class Resume { public int ID {get; set;} // or should it be: public virtual Employee EmployeePerson? // --> yep, easier for you, innit? public Employee Employee {get; set;} // or should it be: public virtual Job UserJob? --> yep :) public virtual Job Job {get; set;} public DateTime EmploymentDate {get; set;} } public class Employee { public int EmployeeID {get; set;} // or should it be: public virtual MembershipUser User? // --> yes, as an approach, but read on for clarification. public virtual MembershipUser User {get; set;} // Is this correct at all? ---> needs to be declared as virtual public virtual ICollection<Resume> Resumes {get; set;} } 

Your Job class is okay as far as I can tell.

To be clear, it will work using it the same way as at the beginning, but you need to explicitly specify the properties using ForeignKeyAttribute , which is not necessary if you are ready to give up tiny control over how FK are named in your database.

  • One note about MembershipUser navigation properties: I'm afraid you will need to write a wrapper for the MembershipUser class because, as far as I remember, it does not have a default constructor and therefore EF will probably not be able to create an instance for you on lazy loading.

  • On the effect of marking a property as virtual , you can take a look at this question: What effects can a virtual keyword have in Entity Framework 4.1 POCO Code First?

+5
source

In the case of code, I would probably use this first:

 public class Job { public virtual int JobId {get; set;} public virtual string JobTitle {get; set;} public virtual ICollection<Resume> Resumes {get; set;} } public class Resume { [Key, Column(Order = 0)] public virtual int EmployeeId {get; set;} [Key, Column(Order = 1)] public virtual int JobId {get; set;} public virtual DateTime EmploymentDate {get; set;} public virtual Employee Employee {get; set;} public virtual Job Job {get; set;} } public class Employee { public virtual int EmployeeId {get; set;} public virtual int UserId {ger; set;} public virtual User User {get;set;} public virtual ICollection<Resume> Resumes {get; set;} } 

Foreign keys are essentially bad, but they make it much easier to work in EF. If you do not use a foreign key property, EF will define various types of relationships . The virtual keyword for the navigation property for lazy loading and the virtual keyword for other associated properties is for tracking changes.

+3
source

Source: https://habr.com/ru/post/885715/


All Articles