我有下一个实体配置:
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; }
}
但它为expens日志ExpenseLogs和ExpenseLogs1创建了两个表。 ExpenseLogs只有外键ExpenseId和OfficeId。 ExpenseLogs1具有与ExpenseLog类相同的字段。我也尝试使用下一个映射,但它没有帮助:
modelBuilder.Entity<ExpenseLog>().HasRequired(e => e.Office);
modelBuilder.Entity<ExpenseLog>().HasRequired(e => e.Expense);
答案 0 :(得分:0)
您有一些冗余配置请尝试以下更改:
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; }
}
请参阅Create code first, many to many, with additional fields in association table