
1 #region 实体映射 2 public DbSet<UserInfo> UserInfos { get; set; } 3 public DbSet<SiteInfo> SiteInfos { get; set; } 4 public DbSet<ParentBanKuai> ParentBanKuais { get; set; } 5 public DbSet<NewInfo> NewInfos { get; set; } 6 public DbSet<GgType> GgTypes { get; set; } 7 public DbSet<AdvertisingInfo> AdvertisingInfos { get; set; } 8 public DbSet<FriendLinks> FriendLinkss { get; set; } 9 public DbSet<ChildBanKuai> ChildBanKuai { get; set; } 10 #endregion
#region 覆盖默认的约定----默认情况下EF将实体映射到数据库中dbo架构下的同名表上 /// <summary> /// 覆盖默认的约定 /// </summary> /// <param name="modelBuilder"></param> protected override void OnModelCreating(DbModelBuilder modelBuilder) { //移除EF映射默认给表名添加“s“或者“es” modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); 解决办法 modelBuilder.Entity<UserInfo>().HasKey(u => u.Id); //modelBuilder.Entity<UserInfo>().HasKey(u => u.Id).ToTable("UserInfo"); modelBuilder.Entity<SiteInfo>().HasKey(s=>s.Id); modelBuilder.Entity<ParentBanKuai>().HasKey(p=>p.Id); modelBuilder.Entity<NewInfo>().HasKey(n => n.Id); modelBuilder.Entity<GgType>().HasKey(g => g.Id); modelBuilder.Entity<AdvertisingInfo>().HasKey(g => g.Id).HasRequired(a=>a.GgType); modelBuilder.Entity<FriendLinks>().HasKey(f => f.Id); //modelBuilder.Entity<ChildBanKuai>().HasKey(c => c.Id); modelBuilder.Entity<ChildBanKuai>().HasKey(c => c.Id).HasRequired(c => c.ParentBanKuai); modelBuilder.Entity<ChildBanKuai>().ToTable("ChildBanKuai"); base.OnModelCreating(modelBuilder); } #endregion
“/”应用程序中的服务器错误。 对象名'dbo.NewInfo无效。