【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 ...
随机推荐
- APP端测试,常见功能测试点汇总
除去每个产品和版本不同的业务需求以及功能,针对于大多数的APP的共同点和移动设备的特性,本文总结了一些APP功能测试中经常遇见,需要考虑到的测试点以共参考 一.安装和卸载 应用的安装和卸载在任何一款A ...
- nexus实现从windows迁移至Linux平台
说明: 由于老环境是在本地windows 2008 R2里面搭建的nexus,前面搭建了jenkins,需要将maven私库迁移至云服务器的CentOS 7系统下,之前没做过nexus的迁移,在网上看 ...
- 性能度量RMSE
回归问题的典型性能度量是均方根误差(RMSE:Root Mean Square Error).如下公式. m为是你计算RMSE的数据集中instance的数量. x(i)是第i个实例的特征值向量 ,y ...
- 当Kubernets遇上阿里云 -之七层负载均衡(一).
我们知道Kubernetes的service只能实现基于4层的负载均衡,无法提供7层之上的许多特性,诸如基于URL的负载均衡,SSL支持,三方授权等等:Ingress可以实现七层负载均衡的许多功能,唯 ...
- 软件工程-东北师大站-第六次作业PSP
1.本周PSP 2.本周进度条 3.本周累计进度图 代码累计折线图 博文字数累计折线图 4.本周PSP饼状图
- 希尔排序(java实现)
上篇blog中介绍的直接插入排序,希尔排序就是对直接插入排序的一个优化.比如有这么一种情况:对一个无序数组进行从小到大的排序,但是数组的最后一个位置的数是最小的,我们要把它挪到第一个位置,其他位置的都 ...
- eclipse连接SQL2008R2
最近又开始写JAVA WEB了,想起连接数据库就麻烦,但是通过一天的努力我居然弄好了,很有成就感. 我用的是 SQL Server 2008 R2 + eclipse 首先要成功的安装好SQL最终 ...
- Java微笔记(9)
使用 Date 和 SimpleDateFormat 类表示时间 处理日期和时间的相关数据,可以使用 java.util 包中的 Date 类 使用 Date 类的默认无参构造方法创建出的对象就代表当 ...
- unrecognized selector send to instancd 快速定位
1.在Debug菜单中Breakpoints->Create Symbolic Breakpoint; 2.在Symbolic中填写方法签名: -[NSObject(NSObject) does ...
- VC++中使用用户自定义消息及自定制窗口技巧
Windows 应用程序所要做的每项工作几乎都是基于消息处理的, Windows 系统消息分为常用 Windows 消息,控件通知消息和命令.然而,有时我们需要定义自己的消息来通知程序什么事情发生了, ...