EF使用CodeFirst方式生成数据库技巧经验

前言

EF已经发布很久了,也有越来越多的人在使用EF。如果你已经能够非常熟练的使用EF的功能,那么就不需要看了。本文意在将自己使用EF的方式记录下来备忘,也是为了给刚刚入门的同学一些指导。看完此文,你应该就学会以CodeFirst的方式操作数据库了。

本文主要内容

  • CodeFirst生成数据库的流程
  • 初始化配置
  • 数据库实体构造技巧
  • 主外键设置
  • decimal精度修改

项目框架搭建

本文所使用的开发工具是vs2015(EF6.1.3)

第一步:新建一个空白项目

第二步:引用EntityFramework

 

DbContext的初始化配置

 DbContext作为操作数据库的网关,十分重要。我们需要对它进行一些类的初始化操作,例如:解决团队开发中,多人迁移数据库造成的修改覆盖问题。

代码如下:

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity.ModelConfiguration.Conventions;
namespace EFDemo.Core.EF
{/// <summary>/// EF访问数据库的接口   /// </summary>public class MyDbContext : System.Data.Entity.DbContext{        public MyDbContext(): base("EFDemo"){//解决团队开发中,多人迁移数据库造成的修改覆盖问题。Database.SetInitializer<MyDbContext>(null);//base.Configuration.AutoDetectChangesEnabled = false;////关闭EF6.x 默认自动生成null判断语句//base.Configuration.UseDatabaseNullSemantics = true;           
        }    public MyDbContext(System.Data.Common.DbConnection oConnection): base(oConnection, true){this.Configuration.LazyLoadingEnabled = true;         }protected override void OnModelCreating(DbModelBuilder modelBuilder){//表名不用复数形式modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();//移除一对多的级联删除约定,想要级联删除可以在 EntityTypeConfiguration<TEntity>的实现类中进行控制modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();//多对多启用级联删除约定,不想级联删除可以在删除前判断关联的数据进行拦截modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();          base.OnModelCreating(modelBuilder);}//将实体对象写在这里,就可以生成对应的数据。 如下://public DbSet<Demo> Demo { get; set; }
}}

项目位置:

Migrations下Configuration类的初始化配置

 Configuration类的初始化配置十分重要,我们需要通过配置解决一系列迁移问题。例如:允许自动迁移,自动迁移默认情况下不扔掉列在我们的数据库中的表。如果我们不希望这样的行为,我们可以告诉迁移明确允许数据丢失的配置类的AutomaticMigrationDataLossAllowed属性设置为true。

代码如下:

namespace EFDemo.Core.Migrations
{using System;using System.Data.Entity;using System.Data.Entity.Migrations;using System.Linq;internal sealed class Configuration : DbMigrationsConfiguration<EFDemo.Core.EF.MyDbContext>{public Configuration(){//允许自动迁移//不然会报错Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.You can use the Add-Migration command to write the pending model changes to a code-based migration.//允许自动迁移AutomaticMigrationsEnabled = true;//自动迁移默认情况下不扔掉列在我们的数据库中的表。如果我们不希望这样的行为,我们可以告诉迁移明确允许数据丢失的配置类的AutomaticMigrationDataLossAllowed属性设置为true。AutomaticMigrationDataLossAllowed = true;}protected override void Seed(EF.MyDbContext context){//  This method will be called after migrating to the latest version.//  You can use the DbSet<T>.AddOrUpdate() helper extension method //  to avoid creating duplicate seed data. E.g.////    context.People.AddOrUpdate(//      p => p.FullName,//      new Person { FullName = "Andrew Peters" },//      new Person { FullName = "Brice Lambson" },//      new Person { FullName = "Rowan Miller" }//    );//
        }}
}

项目位置:

数据库对应实体对象的定义

 CodeFirst模式需要我们先定义实体,然后通过实体生成数据。

通常我们设计数据库表时,每个表都有(ID,是否删除,备注,添加人,添加时间,修改人,修改时间)等字段。我们可以用基类处理

基类实体:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace EFDemo.Core.Entity
{public class BaseEntity{}[Serializable]public class BaseEntity<TKey> : BaseEntity{public BaseEntity(){this.AddTime = DateTime.Now;}[Key][Display(Name = "编号")]public TKey ID { get; set; }[Display(Name = "排序")][Required(ErrorMessage = "{0}是必填项"), Range(0, int.MaxValue, ErrorMessage = "{0}的范围是{1}到{2}")][DefaultValue(0)]public int Sort { get; set; }[Display(Name = "备注")][MaxLength(256, ErrorMessage = "{0}最大长度{1}")]public string Remark { get; set; }[Display(Name = "是否删除")][Required]public bool Deleted { get; set; }public int AddUser { get; set; }[Display(Name = "添加时间")][DisplayFormat(ApplyFormatInEditMode = true, ConvertEmptyStringToNull = true, DataFormatString = "{0:yyyy-MM-dd HH mm}", HtmlEncode = false, NullDisplayText = "数据无效")]public DateTime AddTime { get; set; }public int ModUser { get; set; }[DisplayFormat(ApplyFormatInEditMode = true, ConvertEmptyStringToNull = true, DataFormatString = "{0:yyyy-MM-dd  HH mm}", HtmlEncode = false, NullDisplayText = "数据无效")]public DateTime? ModTime { get; set; }}}
View Code

省市区表:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace EFDemo.Core.Entity
{/// <summary>/// 省市区/// </summary>public class Public_Area{        [Key]public Guid ID { get; set; }[Display(Name = "父亲ID")]public Guid ParentID { get; set; }[Display(Name = "名称")][MaxLength(32, ErrorMessage = "{0}最大长度{1}")]public String Name { get; set; }}
}
View Code

班级表:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace EFDemo.Core.Entity
{/// <summary>/// 班级/// </summary>public class T_Classes : BaseEntity<int>{public T_Classes() {this.T_Student = new List<Entity.T_Student>();}[InverseProperty("T_Classes")]public virtual List<T_Student> T_Student { get; set; }[Display(Name = "班级名称")][Required(ErrorMessage = "{0}是必填项")][MaxLength(8, ErrorMessage = "{0}最大长度{1}")]public string Name { get; set; }[Display(Name = "人数")]public int Count { get; set; }[Display(Name = "班级经费")]public decimal Money { get; set; }}
}
View Code

 

学生表:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace EFDemo.Core.Entity
{/// <summary>/// 学生/// </summary>public class T_Student : BaseEntity<int>{/// <summary>/// 外键/// </summary>[ForeignKey("ClassesID")]public virtual T_Classes T_Classes { get; set; }[Display(Name = "班级ID")]public int ClassesID { get; set; }[Display(Name = "姓名")][Required(ErrorMessage = "{0}是必填项")][MaxLength(8, ErrorMessage = "{0}最大长度{1}")]public string Name { get; set; }[Display(Name = "性别")]public bool Sex { get; set; }[Display(Name = "年龄")]public int Age { get; set; }[Display(Name = "电话")][Required(ErrorMessage = "{0}是必填项")][RegularExpression(@"^(13[0-9]|15[0-9]|18[0-9])\d{8}$", ErrorMessage = "不是手机号格式")][MaxLength(11, ErrorMessage = "{0}最大长度{1}")]public string Phone { get; set; }/// <summary>/// 省市县外键/// </summary>
[ForeignKey("ProvinceID")]public virtual Public_Area Public_Area_Province { get; set; }[Display(Name = "省ID")]public Guid ProvinceID { get; set; }[ForeignKey("CityID")]public virtual Public_Area Public_Area_City { get; set; }[Display(Name = "市ID")]public Guid CityID { get; set; }[ForeignKey("CountyID")]public virtual Public_Area Public_Area_County { get; set; }[Display(Name = "县ID")]public Guid CountyID { get; set; }}
}
View Code

 

班级表和学生表是一对多的关系,省市区表和学生表是一对多的关系,同时学生表中有多个省市区表的外键。

项目位置:

 

使用命令生成数据库

第一步:将实体对象加入到DbContext中,

如下:

//将实体对象写在这里,就可以生成对应的数据。 如下://public DbSet<Demo> Demo { get; set; }public DbSet<Public_Area> Public_Area { get; set; }public DbSet<T_Classes> T_Classes { get; set; }public DbSet<T_Student> T_Student { get; set; }

第二步:在EFDemo.Core项目下的App.config文件夹中添加生成数据库的配置项

 

 

配置文件代码如下:

name="EFDemo"中的EFDemo要和DbContex中的一致。 
<?xml version="1.0" encoding="utf-8"?>
<configuration><configSections><!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --><section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /></configSections><connectionStrings><!--生成数据库的连接字符串--><add name="EFDemo" connectionString="Data Source=.;Initial Catalog=EFDemoDB;User ID=sa;Password=123456;MultipleActiveResultSets=True;Application Name=EntityFramework" providerName="System.Data.SqlClient" /></connectionStrings><entityFramework><defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"><parameters><parameter value="mssqllocaldb" /></parameters></defaultConnectionFactory><providers><provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /></providers></entityFramework>
</configuration>

 

第三步:执行更新命令

启动项目一定要选择EFDemo.Core

生成数据库如下:

主外键关系设置

 班级和学生一对多关系的设置:

一个表中的多个外键是另一个表中的主键的情况:学生表和省市县表

生成的数据库如下:

需要注意的是:这种情况,在Public_Area表中不能反向设置 public virtual List<T_Student> T_Student ,不然会报错。

 

decimal怎么保存四位小数  

 decimal默认保留两位小数,我们需要通过如下设置让其保留四位小数。

在DbContext中配置班级表中的Money字段让其保留四位小数:

 执行Update-Database命令:

结果如下:

 

 

  

使用EF操作数据库数据

第一步:

 让EFDemo.Web应用EFDemo.Core程序集。

第二步:

配置EFDemo.Web中的webconfig中的数据库连接字符串:

 <connectionStrings><!--操作数据库的连接字符串--><add name="EFDemo" connectionString="Data Source=.;Initial Catalog=EFDemoDB;User ID=sa;Password=123456;MultipleActiveResultSets=True;Application Name=EntityFramework" providerName="System.Data.SqlClient" /></connectionStrings>

第三步:

使用EF向数据库添加数据

    public ActionResult Index(){using (var db = new Core.EF.MyDbContext()){Public_Area area1 = new Public_Area(){ID = Guid.NewGuid(),Name = "河南",ParentID = Guid.NewGuid()};db.Public_Area.Add(area1);Public_Area area2 = new Public_Area(){ID = Guid.NewGuid(),Name = "郑州",ParentID = area1.ID};db.Public_Area.Add(area2);Public_Area area3 = new Public_Area(){ID = Guid.NewGuid(),Name = "新郑",ParentID = area2.ID};db.Public_Area.Add(area3);//添加测试数据T_Classes classes = new T_Classes(){Name = "高中三班",Money = 2000};db.T_Classes.Add(classes);T_Student student = new T_Student(){ClassesID = classes.ID,Name = "张三",Phone = "15236265820",Sex = true,ProvinceID = area1.ID,CityID = area2.ID,CountyID = area3.ID,};db.T_Student.Add(student);db.SaveChanges();}return View();}

第四步:

查看数据库数据

 

Demo完整代码下载

EFDemo.Core.7z

 

下一篇:EF查询之性能优化技巧

 

转载于:https://www.cnblogs.com/eggTwo/p/5959158.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/542404.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java jar包示例_Java包getImplementationVersion()方法和示例

java jar包示例包类的getImplementationVersion()方法 (Package Class getImplementationVersion() method) getImplementationVersion() method is available in java.lang package. getImplementationVersion()方法在java.lang包中可用。 getImplementationVersion() method …

c语言中字母的定义,c语言字符串定义与初始化 - 且听风吟

字符串的两种定义方式char数组char sa[] “hello world”;char指针char *sp “hello world”;这两种方式都产生了一个”hello world”的字符串常量,字符串常量存储在静态存储区中&#xff0c;静态存储区中的内容在程序运行的整个过程中都存在&#xff0c;而且只存储一份。数组…

python计算两字符串中的位置_python – 计算两个字符串之间距离的算法

是否有任何字符串距离算法没有考虑到单词的顺序&#xff1f;以下算法未提供所需结果(在该示例中,所需结果应为1)&#xff1a;import jarojaro.jaro_winkler_metric(uMichael Jordan,uJordan Michael)>>>0.47import LevenshteinLevenshtein.ratio(Michael Jordan,Jorda…

php unset函数_PHP | 使用unset()函数从数组中删除元素

php unset函数Given an array and we have to remove an element from the array. 给定一个数组&#xff0c;我们必须从数组中删除一个元素。 unset()函数 (unset() function) To remove an element from an array, we can use a PHP library unset() function, it accepts th…

vi显示行号

vi显示行号 :set nu 带行号查看&#xff0c;并不改变文件内容:set nonu 取消带行号查看在每个用户的主目录下,都有一个 vi 的配置文件".vimrc"或".exrc"用户可以编辑它,使这些设置在每次启动 vi 时,都有效.例如,加入如下设置行:set nu 显示行号…

对象过滤某个属性 循环 php_37道PHP面试题(附答案)

1、什么事面向对象&#xff1f;主要特征是什么&#xff1f;面向对象是程序的一种设计方式&#xff0c;它利于提高程序的重用性&#xff0c;使程序结构更加清晰。主要特征&#xff1a;封装、继承、多态。2、SESSION 与 COOKIE的区别是什么&#xff0c;请从协议&#xff0c;产生的…

项响琴C语言书籍在线浏览,电子琴 c语言程序

实用#include unsigned char code table[]{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};unsigned char temp;unsigned char key;unsigned char i,j;unsigned char STH0;unsigned char STL0;unsigned int code tab[]{64021,64103,64260,…

Java File类boolean createNewFile()方法(带示例)

文件类布尔型createNewFile() (File Class boolean createNewFile()) This method is available in package java.io.File.createNewFile(). 软件包java.io.File.createNewFile()中提供了此方法。 This method is used to create a new file by using createNewFile() method a…

oracle   SQL执行过程

1.sql执行过程1>解析&#xff08;判断对象是否存在&#xff0c;是否有权限查询&#xff0c;语义解析&#xff0c;检查缓存中是否有相同的SQL等等&#xff09;2>优化&#xff08;CBO确定优化模式&#xff0c;确定访问路径&#xff0c;联接顺序&#xff0c;过程中通过很多综…

vue-video-player修改src就会报错_4、修改入口点代码

在riscv上电时&#xff0c;会进行CPU自检&#xff0c;然后跳转到bootloader处执行。bootloader设置好kernel的运行环境后&#xff0c;从硬盘加载kernel到内存&#xff0c;最后再跳转到kernel入口地址。我们采用的bootloader为OpenSBI&#xff0c;被加载到0x80000000地址&#x…

数码管超声波c语言黑51,51单片机开发板-超声波测距-数码管显示

《51单片机开发板-超声波测距-数码管显示》由会员分享&#xff0c;可在线阅读&#xff0c;更多相关《51单片机开发板-超声波测距-数码管显示(16页珍藏版)》请在人人文库网上搜索。1、计算机技术系项目工作报告课程名称单片机开发板设计与制作实训班级学号姓名项目名称超声波测距…

java 方法 示例_Java ArrayDeque带有示例的removeFirstOccurrence()方法

java 方法 示例ArrayDeque类removeFirstOccurrence()方法 (ArrayDeque Class removeFirstOccurrence() method) removeFirstOccurrence() method is available in java.lang package. removeFirstOccurrence()方法在java.lang包中可用。 removeFirstOccurrence() method is use…

社交应用动态九宫格图片的规则

这里主要以微信和QQ空间为作为研究对象&#xff0c;得到的结论如下。 QQ空间里的动态 iOS设备&#xff0c;以iPhone6为分界 iPhone6及以上分辨率的设备&#xff1a; 当宽且高同时 > 512px时&#xff0c;判断 宽/高的比例值&#xff1a;大于 2时&#xff0c;以高度为基准&…

c语言实现链表结构6,用c语言实现的链表结构--数据结构实验

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼#include"stdio.h"//使用new指针来将临时变量重新初始化#include"stdio.h"typedef int ElemType;typedef struct LNode{ElemType data;struct LNode *next;}LNode,*LinkList;void InitList(LinkList &L)//…

splunk中 如何隐藏input_翻糖制作中,如何避免裂缝,如何隐藏裂缝,如何防粘?...

翻糖蛋糕 因精致的样子和栩栩如生的各种造型深得人们的喜爱&#xff0c;它不仅满足了人们对蛋糕口味及装饰日益多样化的需求&#xff0c;同时也在动手制作的过程中&#xff0c;享受到美食与生活的无穷乐趣。不过裂缝&#xff0c;不平整&#xff0c;干燥对翻糖作品来说无疑是噩梦…

Java DataInputStream readUnsignedByte()方法(带示例)

DataInputStream类readUnsignedByte()方法 (DataInputStream Class readUnsignedByte() method) readUnsignedByte() method is available in java.io package. readUnsignedByte()方法在java.io包中可用。 readUnsignedByte() method is used to read 1 byte (i.e. 8 bit) of …

wpf中groupbox有什么用_展示设计中的标摊是什么 用的什么材料

经常听从事展示设计的工作人员说起标摊&#xff0c;那什么是标摊呢&#xff1f;顾名思义&#xff0c;标摊就是通用标准的国际展会摊位的缩写。但是不少人看到干巴巴的词语还是不能理解。那么这篇文章从用途、材料等方面来详细介绍标摊究竟是什么。 标摊的主要材质是什么一般来说…

Java BigInteger类| nextProbablePrime()方法与示例

BigInteger类nextProbablePrime()方法 (BigInteger Class nextProbablePrime() method) nextProbablePrime() method is available in java.math package. nextProbablePrime()方法在java.math包中可用。 nextProbablePrime() method is used to get the next probable prime n…

SQL 行转列的两种做法

if object_id(tb)is not null drop table tbGocreate table tb(姓名 varchar(10),课程 varchar(10),分数 int)insert into tb values(张三,语文,74)insert into tb values(张三,数学,83)insert into tb values(张三,物理,93)insert into tb values(李四,语文,74)insert into tb…

android一个工程的xml怎么引用另外一个工程的xml,如何在Android中使用XML引用库中另一个包的字符串?...

Androiddocumentation告诉我,我可以使用“包名”访问另一个包中的字符串,无论这意味着什么&#xff1a;[:]/所以在我的清单中,我想访问一个字符串,我已将其放在一个单独的库项目中,在com.globalmentor.android包中 – 毕竟我的R类就是这样&#xff1a;android:label"com.g…