1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data.Entity;
  6. using System.Data.Entity.ModelConfiguration;
  7. using System.ComponentModel.DataAnnotations.Schema;
  8.  
  9. namespace GTBlog.Model
  10. {
  11. public class EfDbContext:DbContext
  12. {
  13. public EfDbContext()
  14. : base("connStr")
  15. {
  16.  
  17. }
  18.  
  19. //增加表后在程序包管理控制台中执行下面的命令
  20. //Add-Migration AddBlogUrl //新增一个数据库迁移版本 AddBlogUrl是要新增版本名称,这个名称必须是唯一的,不能重复
  21. //Update-Database //更新数据库
  22.  
  23. public DbSet<User> Users { get; set; }
  24. public DbSet<Category> Categories { get; set; }
  25. public DbSet<Article> Articles { get; set; }
  26. public DbSet<DBInfo> DBInfos { get; set; }
  27. public DbSet<Menu> Menus { get; set; }
  28.  
  29. protected override void OnModelCreating(DbModelBuilder modelBuilder)
  30. {
  31. modelBuilder
  32. .Configurations
  33. .Add(new UserConfiguration())
  34. .Add(new CategoryConfiguration())
  35. .Add(new ArticleConfiguration())
  36. .Add(new DBInfoConfiguration())
  37. .Add(new MenuConfiguration());
  38. base.OnModelCreating(modelBuilder);
  39. }
  40.  
  41. #region UserConfiguration
  42.  
  43. public class UserConfiguration : EntityTypeConfiguration<User>
  44. {
  45. public UserConfiguration()
  46. {
  47. HasKey(c => c.Id);
  48. Property(c => c.Id)
  49. .IsRequired();
  50. Property(c => c.UserName)
  51. .HasMaxLength();
  52. Property(c => c.Password)
  53. .HasMaxLength();
  54. }
  55. }
  56.  
  57. #endregion
  58.  
  59. #region CategoryConfiguration
  60.  
  61. public class CategoryConfiguration : EntityTypeConfiguration<Category>
  62. {
  63. public CategoryConfiguration()
  64. {
  65. HasKey(c => c.Id);
  66. Property(c => c.Id)
  67. .IsRequired();
  68. Property(c => c.Name)
  69. .IsRequired()
  70. .HasMaxLength();
  71. }
  72. }
  73.  
  74. #endregion
  75.  
  76. #region ArticleConfiguration
  77.  
  78. public class ArticleConfiguration : EntityTypeConfiguration<Article>
  79. {
  80. public ArticleConfiguration()
  81. {
  82. HasKey(c => c.Id);
  83. Property(c => c.Id)
  84. .IsRequired();
  85. Property(c => c.Title)
  86. .HasMaxLength();
  87. Property(c => c.Description)
  88. .HasMaxLength();
  89. }
  90. }
  91.  
  92. #endregion
  93.  
  94. #region DBInfoConfiguration
  95.  
  96. public class DBInfoConfiguration : EntityTypeConfiguration<DBInfo>
  97. {
  98. public DBInfoConfiguration()
  99. {
  100. HasKey(c => c.Id);
  101. Property(c => c.Id)
  102. .IsRequired();
  103. Property(c => c.Explain)
  104. .IsRequired()
  105. .HasMaxLength();
  106. }
  107. }
  108.  
  109. #endregion
  110.  
  111. #region MenuConfiguration
  112.  
  113. public class MenuConfiguration : EntityTypeConfiguration<Menu>
  114. {
  115. public MenuConfiguration()
  116. {
  117. HasKey(c => c.Id);
  118. Property(c => c.Id)
  119. .IsRequired();
  120. Property(c => c.Name)
  121. .HasMaxLength();
  122. Property(c => c.Url)
  123. .HasMaxLength();
  124. }
  125. }
  126.  
  127. #endregion
  128.  
  129. }
  130. }

EF Code First教程-02.1 Fluent API约定配置的更多相关文章

  1. EF Code First教程-02 约定配置

    示例: public class Phone { [Key] //主键 public int Id { get; set; } [Required] //不能为空 [MinLength(),MaxLe ...

  2. Entity Framework 实体框架的形成之旅--Code First模式中使用 Fluent API 配置(6)

    在前面的随笔<Entity Framework 实体框架的形成之旅--Code First的框架设计(5)>里介绍了基于Code First模式的实体框架的经验,这种方式自动处理出来的模式 ...

  3. 一起学ASP.NET Core 2.0学习笔记(二): ef core2.0 及mysql provider 、Fluent API相关配置及迁移

    不得不说微软的技术迭代还是很快的,上了微软的船就得跟着她走下去,前文一起学ASP.NET Core 2.0学习笔记(一): CentOS下 .net core2 sdk nginx.superviso ...

  4. 17.翻译系列:将Fluent API的配置迁移到单独的类中【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/move-configurations-to-seperate-class-in-cod ...

  5. Entity Framework Code First (五)Fluent API - 配置关系

    上一篇文章我们讲解了如何用 Fluent API 来配置/映射属性和类型,本文将把重点放在其是如何配置关系的. 文中所使用代码如下 public class Student { public int ...

  6. Entity Framework Code First (五)Fluent API - 配置关系 转载 https://www.cnblogs.com/panchunting/p/entity-framework-code-first-fluent-api-configuring-relationships.html

    上一篇文章我们讲解了如何用 Fluent API 来配置/映射属性和类型,本文将把重点放在其是如何配置关系的. 文中所使用代码如下 public class Student { public int ...

  7. Entity Framework Code First (四)Fluent API - 配置属性/类型

    上篇博文说过当我们定义的类不能遵循约定(Conventions)的时候,Code First 提供了两种方式来配置你的类:DataAnnotations 和 Fluent API, 本文将关注 Flu ...

  8. EF Code First中的主外键约定和一对一、一对多关系的实现

    对于主外键约定的理解,其实是学习实体间一对一和一对多关系的基础. 1.1 主键(Key)约定 主键的默认约定是:只要字段名为--实体名(类名)+"id"(不区分大小写),这就算是默 ...

  9. EF Code First教程-01 创建一个简单的Code First程序

    1 从nuget中搜索并添加EF 2 在app.config或web.config中添加数据库连接 <connectionStrings> <add name="conns ...

随机推荐

  1. HTML第一课总结

    1.图片热点: 规划出图片上的一个区域,可以做出超链接,直接点击图片区域就可以完成跳转的效果. 示例: 2.网页划区: 在一个网页里,规划出一个区域用来展示另一个网页的内容. 示例: 3.网页的拼接: ...

  2. 【液晶模块系列基础视频】5.4.X-GUI字体驱动4

    ============================= 技术论坛:http://www.eeschool.org 博客地址:http://xiaomagee.cnblogs.com 官方网店:ht ...

  3. CKEditor的使用-编辑文本

    官网下载以及演示:http://ckeditor.com/ 引入js <script src="/Example6/ckeditor/ckeditor.js">< ...

  4. ci调用application/views下的css,js,图片资源出现You don't have permission to access CodeIgniter on this server解决

    原因是view文件下面有个.htaccess文件,里面写的是 Deny from all     //拒绝所有请求 自己本地测试的话,就直接去掉,放到服务器就指定application/views文件 ...

  5. org.apache.struts2.json.JSONWriter can not access a member of class

    偶遇一个问题:org.apache.struts2.json.JSONWriter can not access a member of class org.apache.tomcat.dbcp.db ...

  6. 算法与数据结构题目的 PHP 实现:栈和队列 设计一个有 getMin 功能的栈

    刚入手了一本<程序员代码面试指南>,书中题目的代码都是 Java 实现的,琢磨着把这些代码用 PHP 敲一遍,加深印象. 题目:设计一个有 getMin 功能的栈 —— 实现一个特殊的栈, ...

  7. Web 软件测试 Checklist 应用系列,第 1 部分: 数据输入

    Web 软件测试 Checklist 应用系列,第 1 部分: 数据输入 本文为系列文章"Web 软件测试 Checklist 应用系列"中的第一篇.该系列文章旨在阐述 Check ...

  8. IMP-00009: 导出文件异常结束

    今天准备从生产库向测试库进行数据导入,结果在imp导入的时候遇到" IMP-00009: 导出文件异常结束" 错误,google一下,发现可能有如下原因导致 imp的数据太大,没有 ...

  9. ContentType Office

    Office对应ContentType 当从浏览器返回一个文件时,需要指定ContentType,以下是Office2007对应的值: "application/vnd.openxmlfor ...

  10. Apache服务器安装配置

    Apache服务器安装 1.Apache服务器安装      在Linux系统下,apache服务器的安装方式比较灵活,可以使用二进制包安装,比如:rpm包.deb包.已编译好的包.也可以简单的使用y ...