Jerzy Gawor
Jerzy Gawor

Reputation: 151

Code first EF 6 auto generated relation table with additional column

I am trying to create new table with relation many to many in the code first. I have got two tables: Users and Projects. Now, i want to have relation many to many for this two tables. So i do following:

    public class Project
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string PositionCount { get; set; }
    public bool IsActive { get; set; }
    public virtual ICollection<User> Users { get; set; }
}

public class User : IdentityUser<Guid, UserLogin, UserRole, UserClaim>
{
    public override Guid Id { get; set; }
    public string Tags { get; set; }
    public User()
    {
        Id = Guid.NewGuid();
    }

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User, Guid> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }

    public virtual ICollection<Project> Projects { get; set; }
}

public class RecruitmentDbContext : IdentityDbContext<User, MyRole, Guid, UserLogin, UserRole, UserClaim>
{
    public DbSet<Project> Projects { get; set; }
    public DbSet<UserProject> UserProjects { get;set; }

    public RecruitmentDbContext()
        : base("RecruitmentDB")
    {
    }}

I saw on this page that this is enough to create new table with relation many to many. This is true, because everything works properly. But i want to add new column to this auto generated table, with name "ApplicationStatus". I need this column, because i need to know, if for current user, current project has current status :)

So summarizing: I want to have got a table with relation many to many and with additional column named ApplicationStatus. Is it possible to get this on auto generated table, or maybe i should create it by hand somehow?

Upvotes: 0

Views: 183

Answers (1)

Jaliya Udagedara
Jaliya Udagedara

Reputation: 1196

Yes, that is possible. This should help.

Entity Framework : Customized Join Table in a Many to Many Relationship

Upvotes: 1

Related Questions