一、前言

       久闻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的使用的更多相关文章

  1. .NET框架 - NETFramework + API + EF(DBFirst) + MYSQL

    .NET框架 - NETFramework + MVC+ EF(DBFirst) + MYSQL 1. 安装3个MYSQL插件 ①mysql-for-visualstudio-1.2.8    vs的 ...

  2. 个人 WPF+EF(DBFirst) 简单应用开发习惯及EF学习测试(备忘) -- 2

    接上篇:个人 WPF+EF(DBFirst) 简单应用开发习惯及EF学习测试(备忘) -- 1 Step1 在主程序中设置连接数据库 从Model类库的 App.Config 把数据库字符串拷贝出来, ...

  3. 2_MVC+EF+Autofac(dbfirst)轻型项目框架_用户权限验证

    前言 接上面两篇 0_MVC+EF+Autofac(dbfirst)轻型项目框架_基本框架 与 1_MVC+EF+Autofac(dbfirst)轻型项目框架_core层(以登陆为例) .在第一篇中介 ...

  4. 1_MVC+EF+Autofac(dbfirst)轻型项目框架_core层(以登陆为例)

    前言 在上一篇0_MVC+EF+Autofac(dbfirst)轻型项目框架_基本框架中,我已经介绍了这个轻型框架的层次结构,在下面的这篇文章中,我将以教师登陆功能为例,具体来扩充下我的core层的代 ...

  5. 0_MVC+EF+Autofac(dbfirst)轻型项目框架_基本框架

    前言 原来一直使用他人的开源项目框架,异常的定位会很麻烦,甚至不知道这个异常来自我的代码还是这个框架本身.他人的框架有一定的制约性,也有可能是我对那些框架并没深入了解,因为这些开源框架在网上也很难找到 ...

  6. EF(EntityFramework)与mysql使用,乱码问题

    1.中文乱码问题 利用ef更新数据到mysql数据库中,中文就会变成乱码"???",就算把mysql的数据库的编码设置为"utf8"也会变成乱码,从网上查询了下 ...

  7. EF(EntityFramework)与mysql使用,序列化问题[System.ObjectDisposedException]

    在EF 中使用mysql关联取数据时,如果当前实体中包含另一个实体的list成员,而这个成员为空的话,在json序列化的时候就会报错: '((System.Data.Entity.DynamicPro ...

  8. C#中 EF(EntityFramework) 性能优化

    现在工作中很少使用原生的sql了,大多数的时候都在使用EF.刚开始的时候,只是在注重功能的实现,最近一段时间在做服务端接口开发.开发的时候也是像之前一样,键盘噼里啪啦的一顿敲,接口秒秒钟上线,但是到联 ...

  9. EF(EntityFramework) Migrations 迁移

    1.开启程序包管理器控制台 2.安装EntityFramework PM> Install-Package EntityFramework   3.启用迁移 PM> Enable-Migr ...

随机推荐

  1. mybatis拦截器使用

    目录 mybatis 拦截器接口Interceptor spring boot + mybatis整合 创建自己的拦截器MyInterceptor @Intercepts注解 mybatis拦截器入门 ...

  2. Laya 自适应 不拉伸处理

    Laya.init(640, Laya.Browser.width / 640 * 1028, WebGL); Laya.stage.scaleMode = "fixedwidth" ...

  3. 一学就会pip换镜像源

    首先介绍一个国内好用的镜像站 阿里云 http://mirrors.aliyun.com/pypi/simple/ 豆瓣 http://pypi.douban.com/simple/ 清华大学 htt ...

  4. 设计模式C++实现(1)——策略(Strategy)模式

    目录 策略模式 应用案例 实现的关键 Talk is cheap,let's See The Code 设计思想 参考 策略模式 策略模式定义了一系列算法和行为(也就是策略),他们可以在运行时相互替换 ...

  5. HP VC模块Server Profile配置快速参考(With SUS)

    以管理员身份登录VCM 准备进行Server Profiles的配置 在左侧导航栏中找到并点击"Server Profiles",在右侧主窗口的左下角点击"Add&quo ...

  6. [转载] Centos7的安装、Docker1.12.3的安装,以及Docker Swarm集群的简单实例

    1.环境准备 ​ 本文中的案例会有四台机器,他们的Host和IP地址如下 c1 -> 10.0.0.31 c2 -> 10.0.0.32 c3 -> 10.0.0.33 c4 -&g ...

  7. jQuery 判断浏览器

    jQuery 浏览器判断,jQuery提供了一个 jQuery.browser 方法 来判断浏览器 可用值: safari   opera   msie   mozilla 例如:if($.brows ...

  8. python基础-02-while格式化逻辑运算

    python其他知识目录 1.循环打印“我是小马过河” while True:    print('我是小马过河') #4.用while从一打印到10 #5.请通过循环,1 2 3 4 5 6 8 9 ...

  9. java 乐观锁 vs 悲观锁

    在数据库的锁机制中介绍过,数据库管理系统(DBMS)中的并发控制的任务是确保在多个事务同时存取数据库中同一数据时不破坏事务的隔离性和统一性以及数据库的统一性. 悲观锁其实就是 完全同步 比如 sync ...

  10. ES6的新特性(20)—— Module 的加载实现

    Module 的加载实现 上一章介绍了模块的语法,本章介绍如何在浏览器和 Node 之中加载 ES6 模块,以及实际开发中经常遇到的一些问题(比如循环加载). 浏览器加载 传统方法 HTML 网页中, ...