指定数据连接,指定表名,移除表名复数化(表名后面不加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
}
}
}

  

MVC DbContext的更多相关文章

  1. EF和MVC系列文章导航:EF Code First、DbContext、MVC

    对于之前一直使用webForm服务器控件.手写ado.net操作数据库的同学,突然来了EF和MVC,好多新概念泉涌而出,的确犹如当头一棒不知所措.本系列文章可以帮助新手入门并熟练使用EF和MVC,有了 ...

  2. MVC实用架构设计(三)——EF-Code First(1):Repository,UnitOfWork,DbContext

    前言 终于到EF了,实在不好意思,最近有点忙,本篇离上一篇发布已经一个多星期了,工作中的小迭代告一段落,终于有点时间来继续我们的架构设计了,在这里先对大家表示歉意. 其实这段时间我并不是把这个系列给忘 ...

  3. MVC初学 - The type or namespace name 'DbContext' could not be found

    问题: The type or namespace name 'DbContext' could not be found (are you missing a using directive or ...

  4. .Net Core MVC 网站开发(Ninesky) 2.4、添加栏目与异步方法

    在2.3中完成依赖注入后,这次主要实现栏目的添加功能.按照前面思路栏目有三种类型,常规栏目即可以添加子栏目也可以选择是否添加内容,内容又可以分文章或其他类型,所以还要添加一个模块功能.这次主要实现栏目 ...

  5. ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第二章:利用模型类创建视图、控制器和数据库

    在这一章中,我们将直接进入项目,并且为产品和分类添加一些基本的模型类.我们将在Entity Framework的代码优先模式下,利用这些模型类创建一个数据库.我们还将学习如何在代码中创建数据库上下文类 ...

  6. .Net Core MVC 网站开发(Ninesky) 2.3、项目架构调整-控制反转和依赖注入的使用

    再次调整项目架构是因为和群友dezhou的一次聊天,我原来的想法是项目尽量做简单点别搞太复杂了,仅使用了DbContext的注入,其他的也没有写接口耦合度很高.和dezhou聊过之后我仔细考虑了一下, ...

  7. .Net Core MVC 网站开发(Ninesky) 2.2、栏目管理功能-System区域添加

    在asp或asp.net中为了方便网站的结构清晰,通常把具有类似功能的页面放到一个文件夹中,用户管理功能都放在Admin文件夹下,用户功能都放在Member文件夹下,在MVC中,通常使用区域(Area ...

  8. MVC Core 网站开发(Ninesky) 2.1、栏目的前台显示

    上次创建了栏目模型,这次主要做栏目的前台显示.涉及到数据存储层.业务逻辑层和Web层.用到了迁移,更新数据库和注入的一些内容. 一.添加数据存储层 1.添加Ninesky.DataLibrary(与上 ...

  9. MVC CodeFirst简单的创建数据库(非常详细的步骤)

       最近在学习MVC的开发,相信有过开发经验的人初学一个新的框架时候的想法跟我一样最关心的就是这个框架如何架构,每个架构如何分工,以及最最关键的就是如何与数据库通信,再下来才是学习基础的页面设计啊等 ...

随机推荐

  1. ImageNet 历届冠军最新评析:哪个深度学习模型最适合你?

    原文链接: https://mp.weixin.qq.com/s/I5XgYrPCCGyfV2qTI0sJhQ 深度神经网络自出现以来,已经成为计算机视觉领域一项举足轻重的技术.其中,ImageNet ...

  2. 使用jquery获取url以及jquery获取url参数的方法(转)

    使用jquery获取url以及使用jquery获取url参数是我们经常要用到的操作 1.jquery获取url很简单,代码如下 1.window.location.href; 其实只是用到了javas ...

  3. [CF125E]MST Company

    codeforces description 给出一张\(n\)点\(m\)条边的无向图,求一棵满足\(1\)号点度数恰好为\(k\)的最小生成树,并输出方案. \(1\le k\le n\le500 ...

  4. GNU Radio: Overview of the GNU Radio Scheduler

    Scetion 1: The Flowgraph The flowgraph moves data from sources into sinks. 一个流图由多个模块组成,其中一般包括信源(Sour ...

  5. 【linux】文档查看

    cat: [root@localhost test]# cat log2013.log 2013-01 2013-02 2013-03 [root@localhost test]# cat -n lo ...

  6. emacs之配置自动安装脚本

    emacsConfig下建立install目录,结构大概这样 . ├── auto-complete-etags-setting.el ├── auto-complete-setting.el ├── ...

  7. vim之vundle

    git clone https://github.com/gmarik/vundle.git ~/.vim/bundle/vundle,下载到本地 gvim ~/.vimrc set nocompat ...

  8. redis位图

    <?php function frstr($str){ return str_pad($str,8,'0',STR_PAD_LEFT); } $php=''; $p= frstr(decbin( ...

  9. HDU 1686 Oulipo(优化的KMP)

    Oulipo Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  10. [Java.web]MVC 案例-开发用户模块(注册)

    代码下载 生成后的 user.xml 路径在 X:\day09_user\WebRoot\WEB-INF\classes\users.xml 使用测试 在 day09 项目上右键 -> Run ...