One-to-Many Relationship with ApplicationUser class - c#

I have this User class :
public class User
{
public int UserID { get; set; }
public string UserName { get; set; }
public string Email { get; set; }
public virtual ICollection<Project> Projects { get; set; }
}
And this Project Class :
public class Project
{
public int ProjectID { get; set; }
public int UserID { get; set; }
public string ProjectTitle { get; set; }
public virtual User User { get; set; }
}
There is a one-to-many relationship between User and Project as you can see.
Instead of my User class, I want to use Identity's ApplicationUser class. So would it be ok if I change IdentityModels.cs to this :
public class ApplicationUser : IdentityUser
{
public int UserID { get; set; }
public string ProjectTitle { get; set; }
public virtual User User { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
}
And How should I change AccountViewModel.cs? should I add these
public string Email { get; set; }
public int UserID { get; set; }
to every ViewModel in that file?
Finally how should I change Project class? Should I do this :
public class Project
{
public int ProjectID { get; set; }
public int UserID { get; set; }
public string ProjectTitle { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
}
If you can answer to my this long question, I would be very glad. Thanks.

Related

Extending identity User to create a profile

I created a class to represent a user profile. It's also linked to the default asp.net core applicationUser generated:
public class ApplicationUser : IdentityUser
{
[ForeignKey("UserProfile")]
public string UserProfileId { get; set; }
[Required]
public virtual UserProfile UserProfile { get; set; }
}
public class UserProfile
{
public string Id { get; set; }
public string Name { get; set; }
public string Status { get; set; }
[Required]
public virtual ApplicationUser User { get; set; }
}
My question is, do I still need to create a DbSet for ApplicationUser?
public DbSet<UserProfile> UserProfiles { get; set; }
public DbSet<ApplicationUser> ApplicationUsers { get; set; } //DO I NEED THIS?

how to add a foreign key referance in AspNetUsers in mvc5

Please can anyone help me!
I have a Model class named: Student.
Now i need to save a user with "StudentID". "StudentID" will be saved in user table as foreign key.
here is my Student class
public class Student
{
public int ID { get; set; }
public string LastName { get; set; }
public string FirstMidName { get; set; }
public int? DepID { get; set; }
public DateTime EnrollmentDate { get; set; }
[ForeignKey("DepID")]
public virtual Department Department { get; set; }
public virtual ICollection<Enrollment> Enrollments { get; set; }
}
and my identity model is
public class ApplicationUser : IdentityUser
{
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection")
{
}
public DbSet<Student> Students { get; set; }
public DbSet<Enrollment> Enrollments { get; set; }
public DbSet<Course> Courses { get; set; }
public DbSet<Department> Departments { get; set; }
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
so how can i add "studentID" into user table as foreign key.
In case you just want to use the StudenID as a foreign key in a different table, you can do it like this e.g
public class Employee
{
[Key]
public int EmployeeID { get; set; }
public string Name { get; set; }
public virtual EmployeeDetail EmployeeDetail { get; set; }
}
public class EmployeeDetail
{
[Key]
[ForeignKey("Employee")]
public int EmployeeID { get; set; }
public string Adress { get; set; }
public virtual Employee Employee { get; set; }
}
In case you are talking about the actual User table created by Asp.Net Identity, then you can simply extend and customize the User table:
http://typecastexception.com/post/2014/06/22/ASPNET-Identity-20-Customizing-Users-and-Roles.aspx

EntityFramework code first keys

I have following tables: User, UserGroups, and UserInGroups. You can see them on picture below. When i call User i want to be able to get Groups that user is in (UserInGroups). I am reading materials about EntityFramework but i am unable to make connections in code to to that, what am i missing? Do i need to connect them onModelCreating?
Currently i am not getting any data from UserInGroup or UserGroups.
DB looks like this
My classes look like this:
public class User : BaseEntity
{
public int RoleId { get; set; }
public Role Role { get; set; }
public UserGroup UserGroup { get; set; }
public UserInGroup UserInGroup { get; set; }
}
public class UserGroup : BaseEntity
{
public UserGroup()
{
Users = new List<User>();
UserInGroups = new List<UserInGroup>();
}
[StringLength(255)]
public string Name { get; set; }
public string KeyName { get; set; }
public List<User> Users { get; set; }
public List<UserInGroup> UserInGroups { get; set; }
}
public class UserInGroup : BaseEntity
{
public UserInGroup()
{
Users = new List<User>();
UserGroups = new List<UserGroup>();
}
public int UserId { get; set; }
public User User { get; set; }
public int UserGroupId { get; set; }
public UserGroup UserGroup { get; set; }
public List<User> Users { get; set; }
public List<UserGroup> UserGroups { get; set; }
}
DbContext:
public DbSet<GlobalSettings> GlobalSettings { get; set; }
public DbSet<Role> Roles { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<UserGroup> UserGroups { get; set; }
public DbSet<UserInGroup> UsersInGroups { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<GlobalSettings>().Property(x => x.Key).HasColumnAnnotation("Index", new IndexAnnotation(new[] { new IndexAttribute("Index_VariablenName") { IsClustered = false, IsUnique = true } }));
}
public abstract partial class BaseEntity
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
}
public class User : BaseEntity
{
public string Username { get; set; }
public string Title { get; set; }
public FirstName { get; set; }
public string LasName { get; set; }
public Genders Gender { get; set; }
public string Email { get; set; }
public int RoleId { get; set; }
public string TechUser { get; set; }
public DateTime TechChangeDate { get; set; }
public int TechVersion { get; set; }
public bool IsDeleted { get; set; }
public virtual Role Role { get; set; }
public virtual ICollection<UserInGroup> UserInGroups { get; set; }
}
public class UserInGroup : BaseEntity
{
public int UserId { get; set; }
public int UserGroupId { get; set; }
public string TechUser { get; set; }
public DateTime TechChangeDate { get; set; }
public int TechVersion { get; set; }
public bool IsDeleted { get; set; }
public virtual User User { get; set; }
public virtual UserGroup UserGroup { get; set; }
}
public class UserGroup : BaseEntity
{
public string Name { get; set; }
public string KeyName { get; set; }
public string TechUser { get; set; }
public DateTime TechChangeDate { get; set; }
public int TechVersion { get; set; }
public bool IsDeleted { get; set; }
}
public class Role : BaseEntity
{
public string Name { get; set; }
}
public enum Genders
{
Male = 1,
Female = 2
}
You can use two methods to fill navigation properties. First is lazy-loading and second is explicit specifying of required properties.
For lazy-loading you should declare your navigation properties as virtual:
public class User : BaseEntity
{
public int RoleId { get; set; }
public virtual Role Role { get; set; }
public virtual UserGroup UserGroup { get; set; }
public virtual UserInGroup UserInGroup { get; set; }
}
public class UserGroup : BaseEntity
{
public UserGroup()
{
Users = new HashSet<User>(); // HashSet is more effective than List
UserInGroups = new HashSet<UserInGroup>();
}
[StringLength(255)]
public string Name { get; set; }
public string KeyName { get; set; }
public virtual ICollection<User> Users { get; set; } // ICollection is less restrective
public virtual ICollection<UserInGroup> UserInGroups { get; set; }
}
Now, you can load f.e. single user:
var justUser = dbContext.Users.Single(u => u.Id == 100);
When you need its properties they will by transparently loaded:
foreach (var userInGroup in user.UsersInGroups) // here is second loading
{
. . .
}
The second way is the calling of the Include method to explicit specifying required properties:
var userWithGroups = dbContext.Users
.Include(u => u.UserInGroups) // include single navigation property
.Include(ugs => ugs.Groups.Select(ug => ug.Group)) // include collection navigation property
.Single(u => u.Id == 100); // get the user with specified id and filled specified properties

How to ignore an inner nested object when using AutoMapper

Hello I have the classes:
Class User
public class User
{
public Int64 Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public Profile Profile { get; set; } //EF one to one
}
Class Profile
public class Profile
{
public Int64 Id { get; set; }
public string Skype { get; set; }
public string Phone { get; set; }
public string Mobile { get; set; }
public virtual ICollection<Address> Addresses { get; set; }
public virtual User User { get; set; } //This is because EF Mappings
}
Class User DTO
public class UserDTO
{
public string Name { get; set; }
public string Email { get; set; }
public Profile Profile { get; set; }
}
I did the configurations to Map User to UserDTO
Mapper.CreateMap<User, UserDTO>();
I need to have the Profile.User because of the Entity Framework One to One Relationship but I don't want the Profile.User to be shown in the Mapping.
How can I ignore the Profile.User?
You could use a UserProfileDTO class that omits User
public class UserProfileDTO
{
public string Skype { get; set; }
public string Phone { get; set; }
public string Mobile { get; set; }
public ICollection<AddressDTO> Addresses { get; set; }
}
public class UserDTO
{
public string Name { get; set; }
public string Email { get; set; }
public UserProfileDTO Profile { get; set; }
}
Mapper.CreateMap<User, UserDTO>();
Mapper.CreateMap<Profile, UserProfileDTO>();

Entity Framework - Store parent reference on child relationship (one -> many)

I have a setup like this:
[Table("tablename...")]
public class Branch
{
public Branch()
{
Users = new List<User>();
}
[Key]
public int Id { get; set; }
public string Name { get; set; }
public List<User> Users { get; set; }
}
[Table("tablename...")]
public class User
{
[Key]
public int Id {get; set; }
public string Username { get; set; }
public string Password { get; set; }
[ForeignKey("ParentBranch")]
public int? ParentBranchId { get; set; } // Is this possible?
public Branch ParentBranch { get; set; } // ???
}
Is it possible for the User to know what parent branch it belongs to? The code above does not populate the ParentBranch.
Entity Framework version 5.0
.NET 4.0
c#
Try making navigation properties virtual,
[Table("tablename...")]
public class Branch
{
public Branch()
{
Users = new List<User>();
}
[Key]
public int Id { get; set; }
public string Name { get; set; }
public virtual List<User> Users { get; set; }
}
[Table("tablename...")]
public class User
{
[Key]
public int Id {get; set; }
public string Username { get; set; }
public string Password { get; set; }
[ForeignKey("ParentBranch")]
public int? ParentBranchId { get; set; } // Is this possible?
public virtual Branch ParentBranch { get; set; } // ???
}

Categories