【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 ...
随机推荐
- js创建对象 object.create()用法
Object.create()方法是ECMAScript 5中新增的方法,这个方法用于创建一个新对象.被创建的对象继承另一个对象的原型,在创建新对象时可以指定一些属性. 语法: Object.crea ...
- ubuntu的学习教程(常用操作)
摘要 最近在学习linux,把自己学习过程中遇到的常用操作以及一些有助于理解的内容记录下来.我主要用的是ubuntu系统 命令提示符 '~' 这个是指用户的家目录,用户分为root用户和普通用户,ro ...
- 微服务介绍及Asp.net Core实战项目系列之微服务介绍
0.目录 整体架构目录:ASP.NET Core分布式项目实战-目录 一.微服务选型 在做微服务架构的技术选型的时候,我们以“无侵入”和“社区活跃”为主要的考量点,将来升级为原子服务架构.量子服务架构 ...
- Ajax文件上传三式
文件上传(三式) 1.urls.py文件 url(r'^upload.html$', views.upload), 2.views.py文件 import os def upload(request) ...
- Django之视图系统
Django的View(视图) 一个视图函数(类),简称视图,是一个简单的python函数(类),它接受web请求并返回web响应. 响应可以是一张网页的HTML内容,一个重定向,一个404错误,或者 ...
- ubuntu dpkg出现语法错误:安装软件提示无效组件
当安装软件是提示组件错误而导致不能安装,特别是以前卸载软件之后,再安装新版本的软件,其实就是之前卸载的时候没有按照正确的方法卸载引起的 解决方法如下: 使用sudo授权 1. 列出sudo dpkg ...
- vue的ui库使用Element UI,纯html页面,不使用webpack那玩意
使用手册访问:https://cloud.tencent.com/developer/doc/1270 第一步:在head添加样式 <link rel="stylesheet" ...
- VR中射线点击按钮的实现
VR中实现UI的Button点击,主要是需要实现IPointerClickHandler接口,因为在Unity将所有的按钮操作都封装成了相应的接口,需要相应的功能只需要去实现对应的接口就好了.在这里我 ...
- JAVA学习笔记--策略设计模式与适配器模式
一.策略设计模式 创建一个能够根据所传递对象的不同而具有不同行为的方法被称为策略设计模式:这类方法包含所要执行的算法中固定不变的部分,而“策略”包含变化的部分.策略就是传递进去的参数对象,它包含要执行 ...
- Just a Hook:线段树+区间修改
E - Just a Hook In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most ...