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 ...
随机推荐
- 分布式架构高可用架构篇_08_MyCat在MySQL主从复制基础上实现读写分离
参考: 龙果学院http://www.roncoo.com/share.html?hamc=hLPG8QsaaWVOl2Z76wpJHp3JBbZZF%2Bywm5vEfPp9LbLkAjAnB%2B ...
- Editplus从下载到使用
☆ 准备工作 1,保证浏览器正常上网 2,能下载软件或已经下载到Editplus这个工具. ☆ 下载editplus 在浏览器输入http://www.editplus.com,然后回车.进入edit ...
- PHP mkdir 方法 创建 0777 权限的目录问题
php 中使用 mkdir() 方法创建 0777 权限的目录: $path = './Logs/secondCheck/';if(!is_dir($path)){ mkdir($path, 0777 ...
- Javascript 笔记与总结(2-1)Javascript 与 DOM
浏览器有渲染 html 的功能,把 html 源码在内存里形成一个 DOM 对象,就是文档对象. 浏览器内部有一个 js 的解释器 / 执行器 / 引擎,如 Chrome 的 V8 引擎. 在 htm ...
- Web 在线文件管理器学习笔记与总结(4)查看文件内容
② 查看文件内容 a.通过 file_get_contents($filename) 得到文件内容 b.通过 highlight_string($string) 或者 highlight_file($ ...
- java web基础2HTTP协议知识点总结
一.HTTP协议基础 1.定义:HTTP是基于TCP连接的浏览器与服务器通信协议.(即传输层先用TCP三次握手建立连接,进而HTTP通信) 2.连接原理:先进行TCP建立端到端连接,然后发送和接受HT ...
- 【Objective-C】Windows下Objective-C开发环境配置
[Objective-C]Windows下Objective-C开发环境配置 ftp://ftpmain.gnustep.org/pub/gnustep/binaries/windows/ 最近打 ...
- js 创建书签小工具之理论
我们一直在寻找增加浏览体验的方法,有的方法众所周知,有的则鲜为人知.我原本认为书签小工具属于后者,非常令人讨厌的东西.令我非常懊恼的是我发现在这个问题上我完全是错误的.它并不是令人厌烦的,而是以用户为 ...
- PHP date 格式化一个本地时间/日期
PHP date 格式化一个本地时间/日期 date (PHP 4, PHP 5) date — 格式化一个本地时间/日期 说明 string date ( string $format [, int ...
- ESXi云管理平台
实验室有多台使用ESXi实现虚拟化的服务器,平时管理不便,便通实验室其他同学一起编写了一个基于ESXi的云平台管理系统. 对物理服务器进行管理,实现增加.删除.修改.性能监控. 对虚拟机进行管理,实现 ...