0

I have next entity configuration:

public class OfficesContext : DbContext
{
    public DbSet<Office> Offices { get; set; }
    public DbSet<Expense> Expenses { get; set; }
    public DbSet<ExpenseLog> ExpenseLogs { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Office>()
            .Property(o => o.OfficeId)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
        modelBuilder.Entity<Expense>()
                        .Property(o => o.ExpenseId)
                        .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
        modelBuilder.Entity<Office>()
            .HasMany(o => o.Expenses)
            .WithMany()
            .Map(mc =>
            {
                mc.ToTable("ExpenseLogs");
                mc.MapLeftKey("ExpenseId");
                mc.MapRightKey("OfficeId");
            });
    }
}

public class Office
{
    public Office()
    {
        ExpenseLogs = new HashSet<ExpenseLog>();
        Expenses = new HashSet<Expense>();
    }

    public int OfficeId { get; set; }

    public string Name { get; set; }

    public ICollection<ExpenseLog> ExpenseLogs { get; private set; }

    public ICollection<Expense> Expenses { get; private set; } 
}

public class Expense
{
    public Expense()
    {
        ExpenseLogs = new HashSet<ExpenseLog>();
        Offices = new HashSet<Office>();
    }

    public int ExpenseId { get; set; }

    public string Name { get; set; }

    public ICollection<ExpenseLog> ExpenseLogs { get; private set; }

    public ICollection<Office> Offices { get; private set; } 
}

public class ExpenseLog
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ExpenseLogId { get; set; }

    public int OfficeId { get; set; }

    public Office Office { get; set; }

    public int ExpenseId { get; set; }

    public Expense Expense { get; set; }

    public DateTime InputDate { get; set; }

    public decimal Amoun { get; set; }

    public string Description { get; set; }

}

But it creates two tables for expences logs ExpenseLogs and ExpenseLogs1. ExpenseLogs has only foreign keys ExpenseId and OfficeId. ExpenseLogs1 has same fields as in class ExpenseLog. I also tried to use next mappings but it doesnt helped:

modelBuilder.Entity<ExpenseLog>().HasRequired(e => e.Office);
modelBuilder.Entity<ExpenseLog>().HasRequired(e => e.Expense);
Mardok
  • 624
  • 8
  • 18

1 Answers1

0

You have some redundant configuration Try these changes:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Office>()
        .Property(o => o.OfficeId)
        .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
    modelBuilder.Entity<Expense>()
                    .Property(o => o.ExpenseId)
                    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

//   ** this is redundant  - ef will figure it from the bridge class **
//    modelBuilder.Entity<Office>()
//        .HasMany(o => o.Expenses)
//        .WithMany()
//        .Map(mc =>
//        {
//            mc.ToTable("ExpenseLogs");
//            mc.MapLeftKey("ExpenseId");
//            mc.MapRightKey("OfficeId");
//        });
}

public class Office
{
    public Office()
    {
        ExpenseLogs = new HashSet<ExpenseLog>();
        Expenses = new HashSet<Expense>();
    }

    public int OfficeId { get; set; }

    public string Name { get; set; }

    public ICollection<ExpenseLog> ExpenseLogs { get; private set; }

    // access offices thru ExpenseLogs
    // public ICollection<Expense> Expenses { get; private set; } 
}

public class Expense
{
    public Expense()
    {
        ExpenseLogs = new HashSet<ExpenseLog>();
        Offices = new HashSet<Office>();
    }

    public int ExpenseId { get; set; }

    public string Name { get; set; }

    public ICollection<ExpenseLog> ExpenseLogs { get; private set; }

    // access offices thru ExpenseLogs
    // public ICollection<Office> Offices { get; private set; } 
}

See Create code first, many to many, with additional fields in association table

Community
  • 1
  • 1
Steve Greene
  • 12,029
  • 1
  • 33
  • 54
  • This will remove duplicating table, but it will not be many-to-many relation as Expenses should be removed from Office and Offices should be removed from Expense. – Mardok Nov 13 '15 at 08:39
  • Read the link at the bottom. It contains one of the best answers you are ever going to see on SO. If you want properties in your join table it's the only technique currently available through EF6. Not sure about EF7. – Steve Greene Nov 13 '15 at 13:59
  • So it says "it impossible". Please, update you answer with it and I'll mark it as accepted. – Mardok Nov 13 '15 at 18:57