EF Code First教程-02.1 Fluent API约定配置
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Data.Entity;
- using System.Data.Entity.ModelConfiguration;
- using System.ComponentModel.DataAnnotations.Schema;
- namespace GTBlog.Model
- {
- public class EfDbContext:DbContext
- {
- public EfDbContext()
- : base("connStr")
- {
- }
- //增加表后在程序包管理控制台中执行下面的命令
- //Add-Migration AddBlogUrl //新增一个数据库迁移版本 AddBlogUrl是要新增版本名称,这个名称必须是唯一的,不能重复
- //Update-Database //更新数据库
- public DbSet<User> Users { get; set; }
- public DbSet<Category> Categories { get; set; }
- public DbSet<Article> Articles { get; set; }
- public DbSet<DBInfo> DBInfos { get; set; }
- public DbSet<Menu> Menus { get; set; }
- protected override void OnModelCreating(DbModelBuilder modelBuilder)
- {
- modelBuilder
- .Configurations
- .Add(new UserConfiguration())
- .Add(new CategoryConfiguration())
- .Add(new ArticleConfiguration())
- .Add(new DBInfoConfiguration())
- .Add(new MenuConfiguration());
- base.OnModelCreating(modelBuilder);
- }
- #region UserConfiguration
- public class UserConfiguration : EntityTypeConfiguration<User>
- {
- public UserConfiguration()
- {
- HasKey(c => c.Id);
- Property(c => c.Id)
- .IsRequired();
- Property(c => c.UserName)
- .HasMaxLength();
- Property(c => c.Password)
- .HasMaxLength();
- }
- }
- #endregion
- #region CategoryConfiguration
- public class CategoryConfiguration : EntityTypeConfiguration<Category>
- {
- public CategoryConfiguration()
- {
- HasKey(c => c.Id);
- Property(c => c.Id)
- .IsRequired();
- Property(c => c.Name)
- .IsRequired()
- .HasMaxLength();
- }
- }
- #endregion
- #region ArticleConfiguration
- public class ArticleConfiguration : EntityTypeConfiguration<Article>
- {
- public ArticleConfiguration()
- {
- HasKey(c => c.Id);
- Property(c => c.Id)
- .IsRequired();
- Property(c => c.Title)
- .HasMaxLength();
- Property(c => c.Description)
- .HasMaxLength();
- }
- }
- #endregion
- #region DBInfoConfiguration
- public class DBInfoConfiguration : EntityTypeConfiguration<DBInfo>
- {
- public DBInfoConfiguration()
- {
- HasKey(c => c.Id);
- Property(c => c.Id)
- .IsRequired();
- Property(c => c.Explain)
- .IsRequired()
- .HasMaxLength();
- }
- }
- #endregion
- #region MenuConfiguration
- public class MenuConfiguration : EntityTypeConfiguration<Menu>
- {
- public MenuConfiguration()
- {
- HasKey(c => c.Id);
- Property(c => c.Id)
- .IsRequired();
- Property(c => c.Name)
- .HasMaxLength();
- Property(c => c.Url)
- .HasMaxLength();
- }
- }
- #endregion
- }
- }
EF Code First教程-02.1 Fluent API约定配置的更多相关文章
- EF Code First教程-02 约定配置
示例: public class Phone { [Key] //主键 public int Id { get; set; } [Required] //不能为空 [MinLength(),MaxLe ...
- Entity Framework 实体框架的形成之旅--Code First模式中使用 Fluent API 配置(6)
在前面的随笔<Entity Framework 实体框架的形成之旅--Code First的框架设计(5)>里介绍了基于Code First模式的实体框架的经验,这种方式自动处理出来的模式 ...
- 一起学ASP.NET Core 2.0学习笔记(二): ef core2.0 及mysql provider 、Fluent API相关配置及迁移
不得不说微软的技术迭代还是很快的,上了微软的船就得跟着她走下去,前文一起学ASP.NET Core 2.0学习笔记(一): CentOS下 .net core2 sdk nginx.superviso ...
- 17.翻译系列:将Fluent API的配置迁移到单独的类中【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/move-configurations-to-seperate-class-in-cod ...
- Entity Framework Code First (五)Fluent API - 配置关系
上一篇文章我们讲解了如何用 Fluent API 来配置/映射属性和类型,本文将把重点放在其是如何配置关系的. 文中所使用代码如下 public class Student { public int ...
- 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 ...
- Entity Framework Code First (四)Fluent API - 配置属性/类型
上篇博文说过当我们定义的类不能遵循约定(Conventions)的时候,Code First 提供了两种方式来配置你的类:DataAnnotations 和 Fluent API, 本文将关注 Flu ...
- EF Code First中的主外键约定和一对一、一对多关系的实现
对于主外键约定的理解,其实是学习实体间一对一和一对多关系的基础. 1.1 主键(Key)约定 主键的默认约定是:只要字段名为--实体名(类名)+"id"(不区分大小写),这就算是默 ...
- EF Code First教程-01 创建一个简单的Code First程序
1 从nuget中搜索并添加EF 2 在app.config或web.config中添加数据库连接 <connectionStrings> <add name="conns ...
随机推荐
- HTML第一课总结
1.图片热点: 规划出图片上的一个区域,可以做出超链接,直接点击图片区域就可以完成跳转的效果. 示例: 2.网页划区: 在一个网页里,规划出一个区域用来展示另一个网页的内容. 示例: 3.网页的拼接: ...
- 【液晶模块系列基础视频】5.4.X-GUI字体驱动4
============================= 技术论坛:http://www.eeschool.org 博客地址:http://xiaomagee.cnblogs.com 官方网店:ht ...
- CKEditor的使用-编辑文本
官网下载以及演示:http://ckeditor.com/ 引入js <script src="/Example6/ckeditor/ckeditor.js">< ...
- ci调用application/views下的css,js,图片资源出现You don't have permission to access CodeIgniter on this server解决
原因是view文件下面有个.htaccess文件,里面写的是 Deny from all //拒绝所有请求 自己本地测试的话,就直接去掉,放到服务器就指定application/views文件 ...
- 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 ...
- 算法与数据结构题目的 PHP 实现:栈和队列 设计一个有 getMin 功能的栈
刚入手了一本<程序员代码面试指南>,书中题目的代码都是 Java 实现的,琢磨着把这些代码用 PHP 敲一遍,加深印象. 题目:设计一个有 getMin 功能的栈 —— 实现一个特殊的栈, ...
- Web 软件测试 Checklist 应用系列,第 1 部分: 数据输入
Web 软件测试 Checklist 应用系列,第 1 部分: 数据输入 本文为系列文章"Web 软件测试 Checklist 应用系列"中的第一篇.该系列文章旨在阐述 Check ...
- IMP-00009: 导出文件异常结束
今天准备从生产库向测试库进行数据导入,结果在imp导入的时候遇到" IMP-00009: 导出文件异常结束" 错误,google一下,发现可能有如下原因导致 imp的数据太大,没有 ...
- ContentType Office
Office对应ContentType 当从浏览器返回一个文件时,需要指定ContentType,以下是Office2007对应的值: "application/vnd.openxmlfor ...
- Apache服务器安装配置
Apache服务器安装 1.Apache服务器安装 在Linux系统下,apache服务器的安装方式比较灵活,可以使用二进制包安装,比如:rpm包.deb包.已编译好的包.也可以简单的使用y ...