简体   繁体   中英

Change default junction tablename in entity framework 6

I am working with entity framework and I have a many to many relationship using junction tables.

As I understand it should be generated so my context looks like this:

public DbSet<Candidate> Candidates { get; set; }
public DbSet<SkillSet> SkillSets { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Candidate>().HasMany(t => t.SkillSets).WithMany(t => t.Candidates)
        .Map(m =>
        {
            m.ToTable("candidate_skillset");
            m.MapLeftKey("candidate_id");
            m.MapRightKey("skillset_id");
        });

        modelBuilder.Entity<SkillSet>().ToTable("skillset");
        modelBuilder.Entity<Candidate>().ToTable("candidate");
    }

Candidate model:

namespace CandidateCatalog.Model
{
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

[Table("candidate")]
public class Candidate
{ 
    #region Simple propertied

    [Key]
    public int id { get; set; }

    [Column("firstname")]
    public string Firstname { get; set; }
    #endregion

    #region reference properties

    public int? commendation_id { get; set; }
    [ForeignKey("commendation_id")]
    public Commendation commendation { get; set; }

    public ICollection<SkillSet> SkillSets { get; set; }
    #endregion
}

}

Skillset model:

namespace CandidateCatalog.Model
{
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;

[Table("skillset")]
public class SkillSet : SimpleDictionary
{
    public virtual ICollection<Candidate> Candidates { get; set; }
}
}

Entity Framework generated some default junction table name, so as I presume I need to define that name. And here is the problem, how this can be done?

EDIT:

I Have added fluent api;

It can see property:

 public ICollection<SkillSet> SkillSets { get; set; }

But when I try for example:

   var ca = this._catalog.Candidates.FirstOrDefault(x => x.id == 125).SkillSets;

I am getting 0 as if collection is never filled, Ive double check DB relations are there.

You need use FluentAPI like:

Models:

public class Candidate
{
    [Key]
    public int CandidateId { get; set; }

    Column("firstname")]
    public string Firstname { get; set; }


    public virtual ICollection<SkillSet> SkillSets { get; set; }
}



public class SkillSet : SimpleDictionary
{
    [Key]
    public int SkillSetId { get; set; }

    public virtual ICollection<Candidate> Candidates { get; set; }
}

FluentAPI

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{

    modelBuilder.Entity<SkillSet>()
                .HasMany<Candidate>(s => s.Candidates)
                .WithMany(c => c.SkillSets)
                .Map(cs =>
                {
                    cs.MapLeftKey("SkillSetId");
                    cs.MapRightKey("CandidateId");
                    cs.ToTable("candidate_skillset");
                });
    modelBuilder.Entity<SkillSet>().ToTable("skillset");
    modelBuilder.Entity<Candidate>().ToTable("candidate");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM