【EF】EntityFramework DBFirst的使用
一、前言
久闻EF大名,之前做C/S产品用的是Dapper对SqlLite进行ORM。然后接触公司授权系统后发现用的是EntityFramework对SQLSever进行ORM。授权系统里用的是DBFirst,增删查改使用Linq To Entity,觉得非常方便。本篇篇幅较短,老司机可直接略过
二、添加EF
Step1:添加“新建项”,起个名称,添加ADO.NET实体数据模型;
Step2:选择模型类型,来自数据库的EF设计器;
Step3:选择数据连接,新建连接,选择要使用的数据库类型;默认SQLSever;
Step4:测试连接数据库;
Step5:选择EF版本;
Step6:选择要实体化的表,点击完成。
三、操作
经过上述操作会产生三个文件:
1. EntityModel.Context.tt (上下文,所有class的DBSet集合都在这个文件下的.cs文件中)
2. EntityModel.tt (每个表映射后的class都放在这个文件下面)
3. EntityModel.edmx (可视化的表设计器)
假设连接的数据库下有三个表:AgencyInfo,ContractInfo,CustomerInfo。那么EntityModel.tt下就会有三个对应的.cs文件:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------ using System;
using System.Collections.Generic; namespace HHH
{
public partial class AgencyInfo
{
public System.Guid ID { get; set; }
public string UnitName { get; set; }
public string Phone { get; set; }
public string Address { get; set; }
public string comments { get; set; }
public Nullable<System.DateTime> CreatTime { get; set; }
public Nullable<int> ShowFlag { get; set; }
} }
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------ using System;
using System.Collections.Generic; namespace HHH
{
public partial class ContractInfo
{
public System.Guid ID { get; set; }
public string Title { get; set; }
public string Comment { get; set; }
public Nullable<System.DateTime> CreateDate { get; set; }
} }
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------ using System;
using System.Collections.Generic; namespace HHH
{
public partial class CustomerInfo
{
public System.Guid ID { get; set; }
public string Name { get; set; }
public string ContactInfo { get; set; }
public string Address { get; set; }
public string Comments { get; set; }
public string Email { get; set; }
public string MobilePhone { get; set; }
public string province { get; set; }
public string City { get; set; }
public string Type { get; set; }
public Nullable<System.DateTime> CreateDate { get; set; }
} }
那么EntityModel.Context.tt是这样的:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------ using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure; namespace HHH
{
public partial class MyEntities: DbContext
{
public MyEntities()
: base("name=MyEntities")
{
} protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
} public DbSet<ContractInfo> ContractInfoes { get; set; }
public DbSet<CustomerInfo> CustomerInfoes { get; set; }
public DbSet<AgencyInfo> AgencyInfoes { get; set; } }
}
需要对表对象操作时,首先要:
private MyEntities dbContext = new MyEntities();
需要对哪个表操作,就dbContext.ContractInfoes或者dbContext.CustomerInfoes这样找到数据集合,然后用Linq去操作,最后别忘dbContext.SaveChanges()保存修改即可。
四、结尾
明天再看看写点什么,保持学习进度,再过几天要去练车了
【EF】EntityFramework DBFirst的使用的更多相关文章
- .NET框架 - NETFramework + API + EF(DBFirst) + MYSQL
.NET框架 - NETFramework + MVC+ EF(DBFirst) + MYSQL 1. 安装3个MYSQL插件 ①mysql-for-visualstudio-1.2.8 vs的 ...
- 个人 WPF+EF(DBFirst) 简单应用开发习惯及EF学习测试(备忘) -- 2
接上篇:个人 WPF+EF(DBFirst) 简单应用开发习惯及EF学习测试(备忘) -- 1 Step1 在主程序中设置连接数据库 从Model类库的 App.Config 把数据库字符串拷贝出来, ...
- 2_MVC+EF+Autofac(dbfirst)轻型项目框架_用户权限验证
前言 接上面两篇 0_MVC+EF+Autofac(dbfirst)轻型项目框架_基本框架 与 1_MVC+EF+Autofac(dbfirst)轻型项目框架_core层(以登陆为例) .在第一篇中介 ...
- 1_MVC+EF+Autofac(dbfirst)轻型项目框架_core层(以登陆为例)
前言 在上一篇0_MVC+EF+Autofac(dbfirst)轻型项目框架_基本框架中,我已经介绍了这个轻型框架的层次结构,在下面的这篇文章中,我将以教师登陆功能为例,具体来扩充下我的core层的代 ...
- 0_MVC+EF+Autofac(dbfirst)轻型项目框架_基本框架
前言 原来一直使用他人的开源项目框架,异常的定位会很麻烦,甚至不知道这个异常来自我的代码还是这个框架本身.他人的框架有一定的制约性,也有可能是我对那些框架并没深入了解,因为这些开源框架在网上也很难找到 ...
- EF(EntityFramework)与mysql使用,乱码问题
1.中文乱码问题 利用ef更新数据到mysql数据库中,中文就会变成乱码"???",就算把mysql的数据库的编码设置为"utf8"也会变成乱码,从网上查询了下 ...
- EF(EntityFramework)与mysql使用,序列化问题[System.ObjectDisposedException]
在EF 中使用mysql关联取数据时,如果当前实体中包含另一个实体的list成员,而这个成员为空的话,在json序列化的时候就会报错: '((System.Data.Entity.DynamicPro ...
- C#中 EF(EntityFramework) 性能优化
现在工作中很少使用原生的sql了,大多数的时候都在使用EF.刚开始的时候,只是在注重功能的实现,最近一段时间在做服务端接口开发.开发的时候也是像之前一样,键盘噼里啪啦的一顿敲,接口秒秒钟上线,但是到联 ...
- EF(EntityFramework) Migrations 迁移
1.开启程序包管理器控制台 2.安装EntityFramework PM> Install-Package EntityFramework 3.启用迁移 PM> Enable-Migr ...
随机推荐
- CentOS安装输入法及kDE桌面
参考教程:https://jingyan.baidu.com/article/154b46317fdfce28ca8f419e.html
- Jmeter资源监控工具ServerAgent运行原理的一些研究
用过Jmeter的应该都了解,有个ServerAgent工具,放在linux或者windows服务器上开启服务后,在Jmeter中配置下监视器,就可以抓取到服务器的一些资源信息,抓取的主要是cpu.内 ...
- idea 模版之自定义类与方法注释
idea 模版之自定义类与方法注释 很多公司都有要求的代码注释规范,我们每新建类或者方法的时候从新复制粘贴很麻烦,而且容易粘错. 当然自定义模板还可以用到很多地方,比如系统自带的 sout就是syst ...
- 网络流小结(HNOI2019之前)
\(\text{一:Dinic最大流}\) 最坏复杂度 \({\mathcal O(n^2m)}\) 一般可以处理 \(10^4\) ~ \(10^5\) 的网络. struct Edge { int ...
- 请教Amazon FBA里面Label Service, Stickerless, Commingled Inventory是什么意思?
Accept Label Service接受标签服务,选择了以后下面的操作中会有一个让您打印标签的流程,您就可以按照FBA流程提示进行每一步标签服务的操作. Accept Stickless, Com ...
- 王者荣耀交流协会final发布中间产物
WBS+PSP 版本控制报告 软件功能说明书final修订
- 寻找bug
bug1:void不应有返回值. bug2:while(n--)没有条件终止循环. bug3:size和data没有定义 bug4:arr 是sz 在大于0的情况下创建的 一定部位bull 下面的 ...
- 自我介绍for软件工程课程
石家庄铁道大学学生,正在学习软件工程课程. 对于软件工程课程,没什么太大的希望.度了一下,发现软件工程课程近年来比较脱节,这次用新课本不知道效果怎么样.嗯,等课本到手看看再说吧. 自己的目标:我希望能 ...
- Transparent Flow Migration for NFV
Transparent Flow Migration for NFV 摘要 因为SDN提供的灵活性,NF之间存在着流量的迁入和迁出问题.而且NF也要根据相关的状态信息处理数据包,所以流量迁移必须满足以 ...
- ipv6问题
1)百度搜索:针对苹果最新审核要求为应用兼容IPv6 2) ipV6测试网址:http://test-ipv6.com/ http://ipv6.jmu.edu.cn/ http://ipv6test ...