【发布时间】:2016-03-16 09:33:37
【问题描述】:
其他人问过这个问题,我检查了我是否犯了同样的错误,据我所知,我似乎没有。
这是我的 FollowerMenuItemMerchant 类。
public class FollowerMenuItemMerchant
{
[key, Column(Order = 0)]
public int FollowerID { get; set; }
[key, Column(Order = 1)]
public int MenuItemID { get; set; }
[key, Column(Order = 2)]
public int MerchantID { get; set; }
public virtual Follower Follower { get; set; }
public virtual MenuItem MenuItem { get; set; }
public virtual Merchant Merchant { get; set; }
}
这里是 Context 类:
public class FlavorPingContext : IdentityDbContext<ApplicationUser>
{
public FlavorPingContext() : base("name=FlavorPingContext")
{
}
public System.Data.Entity.DbSet<FlavorPing.Models.Merchant> Merchants { get; set; }
public System.Data.Entity.DbSet<FlavorPing.Models.MenuItem> MenuItems { get; set; }
public System.Data.Entity.DbSet<FlavorPing.Models.MerchantDetails> MerchantDetails { get; set; }
public System.Data.Entity.DbSet<FlavorPing.Models.Follower> Followers { get; set; }
public System.Data.Entity.DbSet<FlavorPing.Models.FollowerMenuItemMerchant> FollowerMenuItemMerchants { get; set; }
protected override void OnModelCreating(DbModelBuilder builder) {
// Primary keys
builder.Entity<Follower>().HasKey(q => q.FollowerID);
builder.Entity<MenuItem>().HasKey(q => q.MenuItemID);
builder.Entity<Merchant>().HasKey(q => q.MerchantID);
builder.Entity<FollowerMenuItemMerchant>().HasKey(q =>
new {
q.FollowerID, q.MenuItemID, q.MerchantID
});
// Relationships
builder.Entity<FollowerMenuItemMerchant>()
.HasRequired(t => t.Follower)
.WithMany(t => t.FollowerMenuItemMerchants)
.HasForeignKey(t => t.FollowerID);
builder.Entity<FollowerMenuItemMerchant>()
.HasRequired(t => t.MenuItem)
.WithMany(t => t.FollowerMenuItemMerchants)
.HasForeignKey(t => t.MenuItemID);
builder.Entity<FollowerMenuItemMerchant>()
.HasRequired(t => t.Merchant)
.WithMany(t => t.FollowerMenuItemMerchants)
.HasForeignKey(t => t.MerchantID);
}
}
你能看出哪里可能出错了吗?
【问题讨论】:
标签: entity-framework asp.net-mvc-5