ef 生成mysql数据表_EF使用CodeFirst方式生成数据库&技巧经验

本文详细介绍了如何使用EF6的CodeFirst方法来生成MySQL数据库,包括DbContext初始化配置、自动迁移设置、数据库实体定义及主外键处理,还涉及到decimal精度调整和实际操作数据库的步骤。
摘要由CSDN通过智能技术生成

前言

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

本文主要内容

CodeFirst生成数据库的流程

初始化配置

数据库实体构造技巧

主外键设置

decimal精度修改

项目框架搭建

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

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

56b0e2f35fcfc29ee43d7722fb50d707.png

第二步:引用EntityFramework

accdfb37ff86c02b7e7992bb4b346388.png

0a7da324a40884709248b0f3cb440254.png

DbContext的初始化配置

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

代码如下:

usingSystem;usingSystem.Collections.Generic;usingSystem.Data.Entity;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Data.Entity.ModelConfiguration.Conventions;namespaceEFDemo.Core.EF

{///

///EF访问数据库的接口///

public classMyDbContext : System.Data.Entity.DbContext

{publicMyDbContext()

:base("EFDemo")

{//解决团队开发中,多人迁移数据库造成的修改覆盖问题。

Database.SetInitializer(null);//base.Configuration.AutoDetectChangesEnabled = false;

关闭EF6.x 默认自动生成null判断语句

//base.Configuration.UseDatabaseNullSemantics = true;

}publicMyDbContext(System.Data.Common.DbConnection oConnection)

:base(oConnection, true)

{this.Configuration.LazyLoadingEnabled = true;

}protected override voidOnModelCreating(DbModelBuilder modelBuilder)

{//表名不用复数形式

modelBuilder.Conventions.Remove();//移除一对多的级联删除约定,想要级联删除可以在 EntityTypeConfiguration的实现类中进行控制

modelBuilder.Conventions.Remove();//多对多启用级联删除约定,不想级联删除可以在删除前判断关联的数据进行拦截

modelBuilder.Conventions.Remove();base.OnModelCreating(modelBuilder);

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

}

}

项目位置:

9825ec8c3363f8050448268695a05953.png

Migrations下Configuration类的初始化配置

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

代码如下:

namespaceEFDemo.Core.Migrations

{usingSystem;usingSystem.Data.Entity;usingSystem.Data.Entity.Migrations;usingSystem.Linq;internal sealed class Configuration : DbMigrationsConfiguration{publicConfiguration()

{//允许自动迁移//不然会报错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 voidSeed(EF.MyDbContext context)

{//This method will be called after migrating to the latest version.//You can use the DbSet.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" }//);//}

}

}

项目位置:

117b150189b844326c3ec7eb6d7b3b02.png

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

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

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

基类实体:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.ComponentModel.DataAnnotations;usingSystem.ComponentModel.DataAnnotations.Schema;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceEFDemo.Core.Entity

{public classBaseEntity

{

}

[Serializable]public class BaseEntity: BaseEntity

{publicBaseEntity()

{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

省市区表:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel.DataAnnotations;usingSystem.ComponentModel.DataAnnotations.Schema;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceEFDemo.Core.Entity

{///

///省市区///

public classPublic_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

班级表:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel.DataAnnotations;usingSystem.ComponentModel.DataAnnotations.Schema;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceEFDemo.Core.Entity

{///

///班级///

public class T_Classes : BaseEntity{publicT_Classes() {this.T_Student = new List();

}

[InverseProperty("T_Classes")]public virtual List 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

学生表:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel.DataAnnotations;usingSystem.ComponentModel.DataAnnotations.Schema;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceEFDemo.Core.Entity

{///

///学生///

public class T_Student : BaseEntity{///

///外键///

[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; }///

///省市县外键///

[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

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

项目位置:

de3a64222834308284e5d45e638f197d.png

使用命令生成数据库

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

如下:

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

public DbSet Public_Area { get; set; }public DbSet T_Classes { get; set; }public DbSet T_Student { get; set; }

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

99f91b98096d567e86903f1186063cd3.png

配置文件代码如下:

name="EFDemo"中的EFDemo要和DbContex中的一致。

第三步:执行更新命令

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

7c7ec3d2c2b330fbc1d1481f51e3359b.png

生成数据库如下:

7548ffde22cff9e67cd50b1108e2656a.png

8b48b1cc48f96ab02d460e1f140a48cf.png

主外键关系设置

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

7077e6408236af17a3de64160987ecff.png

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

3711d21913863a2478eb9c14744f1f3e.png

生成的数据库如下:

0738ac1c993ef8df05f6a6fa84bebfbf.png

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

decimal怎么保存四位小数

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

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

b3ff9b4258a5bd727982b971130fa0fd.png

执行Update-Database命令:

结果如下:

381cb3bd39597df66f9c2723e9a73d13.png

使用EF操作数据库数据

第一步:

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

第二步:

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

第三步:

使用EF向数据库添加数据

publicActionResult Index()

{using (var db = newCore.EF.MyDbContext())

{

Public_Area area1= newPublic_Area()

{

ID=Guid.NewGuid(),

Name= "河南",

ParentID=Guid.NewGuid()

};

db.Public_Area.Add(area1);

Public_Area area2= newPublic_Area()

{

ID=Guid.NewGuid(),

Name= "郑州",

ParentID=area1.ID

};

db.Public_Area.Add(area2);

Public_Area area3= newPublic_Area()

{

ID=Guid.NewGuid(),

Name= "新郑",

ParentID=area2.ID

};

db.Public_Area.Add(area3);//添加测试数据

T_Classes classes = newT_Classes()

{

Name= "高中三班",

Money= 2000};

db.T_Classes.Add(classes);

T_Student student= newT_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();

}returnView();

}

第四步:

查看数据库数据

e1128e4f6b367d324bbd0a26f124c06f.png

Demo完整代码下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值