EF CodeFirst的步骤
1 创建各个实体类
2 创建一个空数据模型,然后删除掉,为了引入Entity Framework和System.Data.Entity
3 为实体类增加标注
4 为实体增加导航属性
5 在App.config configuration标签里面增加connectionStrings标签,并增加连接字符串
6 创建数据操作类继承DbContext ,构造函数参数为“name=connStr”
7 增加两个DbSet属性,对应操作的实体
8 重写OnModelCreating方法,使得生成的表不包含复数;
9 调用context.Database.CreateIfNotExists()在对应的数据库下面生成表
10 通过数据操作类操作表
//ClassInfo
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CodeFirstDemo
{
public class ClassInfo
{
[Key]
public int Id { get; set; }
[Required]
[StringLength(32)]
public string ClassName { get; set; }
[Required]
public DateTime CreateTime { get; set; }
public ICollection<StudentInfo> StudentInfo { get; set; }
}
}
//StudentInfo
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CodeFirstDemo
{
public class StudentInfo
{
[Key]
public int Id { get; set; }
[Required]
[StringLength(32)]
public string StudentName { get; set; }
[Required]
public DateTime CreateTime { get; set; }
[Required]
public ClassInfo ClassInfo { get; set; }
}
}
//MyDataContext
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CodeFirstDemo
{
public class MyDataContext:DbContext
{
public MyDataContext()
: base("name = ConnStr")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
public DbSet<ClassInfo> ClassInfo { get; set; }
public DbSet<StudentInfo> StudentInfo { get; set; }
}
}
//主方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CodeFirstDemo
{
class Program
{
static void Main(string[] args)
{
MyDataContext db = new MyDataContext();
db.Database.CreateIfNotExists();
ClassInfo classInfo = new ClassInfo();
classInfo.ClassName = "abc";
classInfo.CreateTime = DateTime.Now;
db.ClassInfo.Add(classInfo);
StudentInfo stu1 = new StudentInfo();
stu1.StudentName = "zhangsan";
stu1.CreateTime = DateTime.Now;
stu1.ClassInfo = classInfo;
StudentInfo stu2 = new StudentInfo();
stu2.StudentName = "Lisi";
stu2.CreateTime = DateTime.Now;
stu2.ClassInfo = classInfo;
db.StudentInfo.Add(stu1);
db.StudentInfo.Add(stu2);
db.SaveChanges();
}
}
}
转载于:https://www.cnblogs.com/dxmfans/p/9434654.html
EF CodeFirst的步骤相关推荐
- MVC3教程之实体模型和EF CodeFirst
在本节中,我们将使用Entity Framework 数据访问技术来定义这些模型类,并对这些类来进行操作.EF支持一个被称之为"code-first"的开发范例.Code-firs ...
- 新年奉献MVC+EF(CodeFirst)+Easyui医药MIS系统
本人闲来无事就把以前用Asp.net做过的一个医药管理信息系统用mvc,ef ,easyui重新做了一下,业务逻辑简化了许多,旨在加深对mvc,ef(codefirst),easyui,AutoMap ...
- EF CodeFirst 如何通过配置自动创建数据库当模型改变时
最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精 本篇为进阶篇,也是弥补自己之前没搞明白的地方,惭愧 ...
- [转]Using Entity Framework (EF) Code-First Migrations in nopCommerce for Fast Customizations
本文转自:https://www.pronopcommerce.com/using-entity-framework-ef-code-first-migrations-in-nopcommerce-f ...
- EF CodeFirst 必须要解决的问题
Entity Framework有三种模式:Model First.DB First和 CodeFirst,这里只谈CodeFirst.实际项目中如果采用了CodeFirst,那么必定会碰见下面这些问 ...
- 了解EF CodeFirst的Migrator功能与Migrator.Net对比
在上一篇[数据库迁移利器:Migrator.Net]中,很多朋友提到了EF的CodeFirst也有数据库的迁移功能,说来真惭愧,玩了那么多年,至今还未去了解EF,今天来了解下CodeFirst然后与M ...
- EF| CodeFirst 代码先行
CodeFirst 实例一 1.新建一个WebForm项目,名字就叫CodeFirstEF 2:在项目中添加引用EF所需要的5个核心类库:(如果找不到这几个类库,可以新建基于数据库的ADO.NET 实 ...
- nopCommerce EF codefirst翻译版
在NopCommerce中使用实体框架(EF)代码优先迁移进行快速自定义 由Woon Cherk Lam | 2013年12月19日,星期四 我见过很多nopCommerce论坛用户询问他们如何使用E ...
- 知其然不知其所以然的悲惨后果【EF CodeFirst 实体关系两日游】
先上测试代码 public class User {public int ID { get; set; } public int BillingAddressID { get; set; }publi ...
最新文章
- Django web框架
- 【Python基础】在pandas中使用数据透视表
- boost::type_erasure::equality_comparable相关的测试程序
- RHEL4-SFTP配置
- 安卓手机系统可删除的自带软件大集合
- 简单介绍Java中Comparable和Comparator
- python 三维地球_用python生成地球运动的动态模拟动态图
- 基于SpringMVC+EasyPoi,采用Excel模板方式实现Excel在线预览和导出(2021版)
- 如何实现微服务架构下的分布式事务?
- 《Act with Prudence》读后感
- hdoj-1046-Gridland(规律题)
- .md文件简单的转化为pdf文件
- 【流媒体开发】9、ffmpeg实现视频录制
- linux scp密码参数,linux-scp不输入密码 - 不断的前进ING。。。 - 51Testing软件测试网 51Testing软件测试网-软件测试人的精神家园...
- Staring into the Abyss: An Evaluation of Concurrency Control with One Thousand Cores 论文阅读
- 风险度量、马科维茨模型的求解与衍生
- 基因组组装(genome assembly)和对应版本的基因注释包(gene annotation packages)
- android图片特效处理之锐化效果
- 骨龄预测代码学习(三)
- 随笔分类 - 深入解析Windows操作系统笔记