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 ...
随机推荐
- C#并行库(TaskParallelLibrary)用法小结
今天有空,总结一下.NET 4.5并行库(TaskParallelLibrary)用法. 也许C和C++的程序员刚刚开始写C#还习惯于new Thread来新建一个线程,但新建线程需要内存和CPU上下 ...
- 【液晶模块系列基础视频】3.4fatfs接口函数的使用4
============================= 技术论坛:http://www.eeschool.org 博客地址:http://xiaomagee.cnblogs.com 官方网店:ht ...
- 小组项目alpha发布的评价
新蜂团队一直都特别努力,这节课我也看到了他们努力的结果--项目可以顺利运行,在两个同学试玩期间没有出现BUG,不知道玩游戏的同学的心情,反正我们在台下看得很开心的. 飞天小女警组的礼物挑选系统,是一个 ...
- HDU 5521 Meeting(虚拟节点+最短路)
Meeting Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Total ...
- 实现ScrollView中包含ListView,动态设置ListView的高度
ScrollView 中包含 ListView 的问题 : ScrollView和ListView会冲突,会导致ListView显示不全 <?xml version="1.0" ...
- unity 环境增强
早上过来解决两个问题: (1)VS2012 配置下 VA_X 插件,现在VS在字体颜色方面已经提高很多,但是在类成员实时显示方面还是不方便,或者我没找到吧 (2)安装unityVs插件,微软把 ...
- 20145235 《Java程序设计》第10周学习总结
教材学习内容总结 网络编程 网络编程对于很多的初学者来说,都是很向往的一种编程技能,但是很多的初学者却因为很长一段时间无法进入网络编程的大门而放弃了对于该部分技术的学习. 程序员所作的事情就是把数据发 ...
- Linux+postfix+extmail+dovecot打造基于web页面的邮件系统
原文地址:http://blog.csdn.net/deansrk/article/details/6717720 最终效果图: 准备阶段:需要手动下载的软件包: postfix-2.6.5.tar. ...
- 医生工作台二期-bug总结
bug记录 1.视频问诊详情查看流,流里进入检查单详情上传图片后返回闪退 这块cell用的相同的id,开始创建的是第一个if里面的cell,后来创建的是else里面的cell,因为之前用TipCell ...
- WCF中自定义消息编码器:压缩编码器的使用
通过抓包知道WCF在提交.返回数据的时候大多使用XML进行数据交互,如果返回DataTable那么这些数据将变得很大,通过查询找到一个对数据压缩的方法: http://msdn.microsoft.c ...