指定数据连接,指定表名,移除表名复数化(表名后面不加s),设置字段约束,主外键关系。

using MvcApplication1.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace MvcApplication1.Repositories
{
    public class EFContext : DbContext
    {
     //指定数据库连接字符串 name是 DefaultConnection public EFContext() : base("DefaultConnection") { } public DbSet<AdminInfo> AdminInfos { get; set; } public DbSet<ArticlesInfo> ArticlesInfos { get; set; } public DbSet<BannerInfo> BannerInfos { get; set; } public DbSet<CandidatesInfo> CandidatesInfos { get; set; } public DbSet<FriendLinksInfo> FriendLinksInfos { get; set; } public DbSet<PartnersInfo> PartnersInfos { get; set; } public DbSet<RecruitmentInfo> RecruitmentInfos { get; set; } public DbSet<SingleArticle> SingleArticles { get; set; } /// <summary> /// 构造表 /// </summary> /// <param name="modelBuilder"></param> protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();//移除表名复数的契约 #region 管理员信息 modelBuilder.Entity<AdminInfo>().HasKey(k => k.ID); //设置主键 modelBuilder.Entity<AdminInfo>().Property(q => q.UserName).IsRequired().HasMaxLength(50);//设置不能为空 modelBuilder.Entity<AdminInfo>().Property(q => q.UserPwd).IsRequired().HasMaxLength(50);//设置不能为空 #endregion #region 招聘信息 modelBuilder.Entity<RecruitmentInfo>().HasKey(k => k.ID); modelBuilder.Entity<RecruitmentInfo>().Property(q => q.TypeID).IsRequired(); modelBuilder.Entity<RecruitmentInfo>().Property(q => q.Title).IsRequired().HasMaxLength(500); modelBuilder.Entity<RecruitmentInfo>().Property(q => q.RequireNum).IsRequired().HasMaxLength(50); modelBuilder.Entity<RecruitmentInfo>().Property(q => q.PostCharacter).IsRequired().HasMaxLength(500); modelBuilder.Entity<RecruitmentInfo>().Property(q => q.Responsibilities).IsRequired().HasMaxLength(500); modelBuilder.Entity<RecruitmentInfo>().Property(q => q.Qualification).IsRequired().HasMaxLength(500); modelBuilder.Entity<RecruitmentInfo>().Property(q => q.CreateDate).IsRequired(); //主表包含多个:CandidatesInfoList;子表含有一个:RecruitmentInfoModel;子表中对应主表的外键:RecruitmentID。 modelBuilder.Entity<RecruitmentInfo>().HasMany(s => s.CandidatesInfoList).WithRequired(c => c.RecruitmentInfoModel).HasForeignKey(f => f.RecruitmentID); #endregion #region 应聘者信息 modelBuilder.Entity<CandidatesInfo>().HasKey(k => k.ID); modelBuilder.Entity<CandidatesInfo>().Property(q => q.RecruitmentID).IsRequired(); modelBuilder.Entity<CandidatesInfo>().Property(q => q.Name).IsRequired().HasMaxLength(500); modelBuilder.Entity<CandidatesInfo>().Property(q => q.MaritalStatus).IsRequired().HasMaxLength(500); modelBuilder.Entity<CandidatesInfo>().Property(q => q.NativePlace).IsRequired().HasMaxLength(500); modelBuilder.Entity<CandidatesInfo>().Property(q => q.DomicileLocation).IsRequired().HasMaxLength(500); modelBuilder.Entity<CandidatesInfo>().Property(q => q.BirthDay).IsRequired().HasMaxLength(500); modelBuilder.Entity<CandidatesInfo>().Property(q => q.Age).IsRequired().HasMaxLength(500); modelBuilder.Entity<CandidatesInfo>().Property(q => q.IDCard).IsRequired().HasMaxLength(500); modelBuilder.Entity<CandidatesInfo>().Property(q => q.Education).IsRequired().HasMaxLength(500); modelBuilder.Entity<CandidatesInfo>().Property(q => q.Hobby).IsRequired().HasMaxLength(500); modelBuilder.Entity<CandidatesInfo>().Property(q => q.Address).IsRequired().HasMaxLength(500); modelBuilder.Entity<CandidatesInfo>().Property(q => q.Areas).IsRequired().HasMaxLength(500); modelBuilder.Entity<CandidatesInfo>().Property(q => q.Email).IsRequired().HasMaxLength(500); modelBuilder.Entity<CandidatesInfo>().Property(q => q.Mobile).IsRequired().HasMaxLength(500); #endregion #region 文章信息 modelBuilder.Entity<ArticlesInfo>().HasKey(k => k.ID);//编号 modelBuilder.Entity<ArticlesInfo>().Property(q => q.TypeID).IsRequired();//类型 1:集团新闻 2:行业动态 modelBuilder.Entity<ArticlesInfo>().Property(q => q.Title).IsRequired();//标题 modelBuilder.Entity<ArticlesInfo>().Property(q => q.IsRecommend).IsRequired();//推荐 0:不推荐 1:推荐 modelBuilder.Entity<ArticlesInfo>().Property(q => q.Img).IsRequired();//配图 modelBuilder.Entity<ArticlesInfo>().Property(q => q.Contents).IsRequired();//内容 modelBuilder.Entity<ArticlesInfo>().Property(q => q.CreateDate).IsRequired();//创建日期 #endregion #region Banner modelBuilder.Entity<BannerInfo>().HasKey(k => k.ID);//编号 modelBuilder.Entity<BannerInfo>().Property(q => q.Name).IsRequired();//名称 modelBuilder.Entity<BannerInfo>().Property(q => q.Img).IsRequired();//图片 modelBuilder.Entity<BannerInfo>().Property(q => q.LinkUrl).IsRequired();//链接 #endregion #region 友情链接表 modelBuilder.Entity<FriendLinksInfo>().HasKey(q => q.ID);//编号 modelBuilder.Entity<FriendLinksInfo>().Property(q => q.Name).IsRequired();//名称 modelBuilder.Entity<FriendLinksInfo>().Property(q => q.LinkUrl).IsRequired();//链接 #endregion #region 合作伙伴表 modelBuilder.Entity<PartnersInfo>().HasKey(q => q.ID);//编号 modelBuilder.Entity<PartnersInfo>().Property(q => q.Name).IsRequired();//名称 modelBuilder.Entity<PartnersInfo>().Property(q => q.Img).IsRequired();//图标 modelBuilder.Entity<PartnersInfo>().Property(q => q.LinkUrl).IsRequired();//链接 #endregion #region 单篇图文信息 modelBuilder.Entity<SingleArticle>().HasKey(q => q.ID);//编号 modelBuilder.Entity<SingleArticle>().Property(q => q.Title).IsRequired();//标题 modelBuilder.Entity<SingleArticle>().Property(q => q.Contents).IsRequired();//内容 modelBuilder.Entity<SingleArticle>().Property(q => q.CreateDate).IsRequired();//创建日期 #endregion #region 添加默认数据 #endregion } } }

  

posted on 2016-06-03 15:18  邢帅杰  阅读(486)  评论(0编辑  收藏  举报