CodeFirst开发方式创建数据库
1)、新建ADO.NET实体数据模型--->选择空CodeFirst模型
2)、新建两个实体类(客户表和订单信息表)
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 CodeFirst开发方式
{
[Table("Customer")]
public class Customer
{ public Customer()
{
this.OrderInfo = new HashSet<OrderInfo>();
}
[Key]
[Required(ErrorMessage ="*必填")]
public int CustomerId { get; set; }
[Required(ErrorMessage = "*必填")]
public string CustomerName { get; set; }
public string CustomerSex { get; set; }
[Required(ErrorMessage = "*必填")]
public int CustomerAge { get; set; }
[Required(ErrorMessage = "*必填")]
[StringLength(,ErrorMessage ="超过最大长度100")]
public string Address { get; set; }
[Required(ErrorMessage = "*必填")]
public string Phone { get; set; }
public string Email { get; set; }
public virtual ICollection<OrderInfo> OrderInfo { get; set; }
}
}
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 CodeFirst开发方式
{
[Table("OrderInfo")]
public class OrderInfo
{
[Key]
[Required(ErrorMessage = "*必填")]
public int OrderId { get; set; }
[Required(ErrorMessage ="*必填")]
public string OrderContent { get; set; }
[Required(ErrorMessage = "*必填")]
public int CustomerId { get; set; }
[Required(ErrorMessage = "*必填")]
public virtual Customer Customer { get; set; }
}
}
3)、修改App.config或Web.config里面的连接字符串
这里name的属性值对象的就是配置文件的连接字符串,将其改成
<add name="DataModel" connectionString="data source=(local);initial catalog=DataModel;persist security info=True;user id=sa;password=123456;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
4)、在新建的CodeFirst空模型类中需要创建的两张表
public virtual DbSet<Customer> Customer { get; set; }
public virtual DbSet<OrderInfo> OrderInfo { get; set; }
5)、如果需要创建的表的主键ID自增,
重写OnModelCreating方法
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//base.OnModelCreating(modelBuilder);
//自增长主键
modelBuilder.Entity<Customer>()
.Property(c => c.CustomerId)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
modelBuilder.Entity<OrderInfo>()
.Property(o => o.OrderId)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
}
6)、在Main中
DataModel db = new DataModel();
Customer cust = new Customer
{
Address = "江西省南昌市瑶湖区",
CustomerAge = ,
CustomerName = "张三",
CustomerSex = "男",
Email = "999999999@qq.com",
Phone = ""
};
db.Customer.Add(cust);
db.SaveChanges(); Console.WriteLine("~~~~~~~OK~~~~~~~~");
Console.ReadKey();
7)、在过程中遇到的一些问题
7.1)、我在已经生成了数据库的时候,修改这一些代码,(已经有数据库了)又重新的运行了一下代码
异常提示信息:The model backing the 'DataModel' context has changed since the database was created. Consider using......
而经过度娘,这篇博客解决了我的问题
https://blog.csdn.net/hit_why/article/details/72778785
其中的解决方案:
这里我给出一种方案,网上也可以搜到:
1. 打开工具-->NuGet包管理器-->程序包管理器控制台
2.在PM>后面输入Enable-Migrations -ContextTypeName DatabaseName(如果执行失败,就参考失败信信息重新输入,失败信息提示的非常明确;DatabaseName是你生成的数据库名字),然后你发现项目里面增加了一个Migrations文件夹,里面自动生成了一些代码,这些代码试根据你的模型和改变生成的
3. 设置Migrations文件夹下面的Configuration文件中的代码:AutomaticMigrationsEnabled=true;也就是将false改为true
4. 在PM>后面输入Add-Migration InitialCreate
5. 在PM>后面 Update-Database -Verbose 或者Update-Database -Verbose -Force强制改变数据库。
之后每次改变模型时,只需在PM>后面 输入Update-Database -Verbose -Force就可以了。
CodeFirst开发方式创建数据库的更多相关文章
- MVC CodeFirst简单的创建数据库(非常详细的步骤)
最近在学习MVC的开发,相信有过开发经验的人初学一个新的框架时候的想法跟我一样最关心的就是这个框架如何架构,每个架构如何分工,以及最最关键的就是如何与数据库通信,再下来才是学习基础的页面设计啊等 ...
- 使用EF CodeFirst 创建数据库
EntityFramework 在VS2015添加新建项时,选择数据->ADO.NET 实体数据模型,有一下选项 来自数据库的EF设计器,这个就是我们最常用的EntityFramework设计模 ...
- EF CodeFirst 如何通过配置自动创建数据库<当模型改变时>
最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精 本篇为进阶篇,也是弥补自己之前没搞明白的地方,惭愧 ...
- EF CodeFirst 创建数据库
最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精 话说EF支持三种模式:Code First M ...
- NetCore2.0下使用EF CodeFirst创建数据库
本文所使用的VS版本:VS2017 15.3.0 首先新建一个.net core项目 取名NetCoreTask 使用模型视图控制器方式 新建Model层 在Model层下新建一个user实体类 1 ...
- 七:mvc使用CodeFirst(代码优先)创建数据库
1. 理解EF CodeFirst模式特点 2. 使用CodeFirst模式生成数据库 1. CodeFirst模式(代码优先) Code First是Entity Framework提供的一种新的编 ...
- ASP.NET Core 2.2 WebApi 系列【二】使用EF CodeFirst创建数据库
Code First模式 Code First是指"代码优先"或"代码先行". Code First模式将会基于编写的类和配置,自动创建模型和数据库. 一.准备 ...
- 2.EF中 Code-First 方式的数据库迁移
原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/code-first-migrations-with-entity-framework/ 系列目 ...
- 【无私分享:ASP.NET CORE 项目实战(第四章)】Code First 创建数据库和数据表
目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 本章我们来介绍下Asp.net Core 使用 CodeFirst 创建数据库和表,通过 控制台 和 dotnet ef 两种 ...
随机推荐
- git clone和git pull的区别
1.需不需要本地文件夹是仓库 git clone是将整个工程复制下来所以,不需要本地是仓库(没有.git文件夹) git clone git pull需要先初始化本地文件夹文一个仓库 git ...
- FCC---Make a CSS Heartbeat using an Infinite Animation Count----超级好看的心跳,粉色的
Here's one more continuous animation example with the animation-iteration-count property that uses t ...
- 关于es6及以上的js编译成es5
问题:es6及以上版本在IE浏览器上不能执行起来,但Chrome浏览器上轻松运行,解决兼容IE的问题就需要使用babel:这个可以去babel的官网去查看; 关于babel的简单使用,有两种方式: 1 ...
- YYLable 的使用 以及注意点
NSString *title = @"不得不说 YYKit第三方框架确实很牛,YYLabel在富文本显示和操作方面相当强大,尤其是其异步渲染,让界面要多流畅有多流畅,这里我们介绍下简单的使 ...
- 关于解决Xcode更新7.3之后插件不能用的问题
Xcode更新7.3之后,之前安装好好的插件现在突然间不能用了(如:我在写背景颜色或者字体颜色的时候,突然间不出来联想的图案来供我选择了),解决这个问题的步骤如下: 1.打开电脑终端,把default ...
- win10安装ubuntu系统出现的一些问题以及解决方案
前言 在win10系统进行安装新的ubuntu环境的时候遇到的一些问题,以及解决方案,供以后参考. 准备 从ubuntu官网下载最近版本的ubuntu系统,Ubuntu最新版本下载地址 操作系统:wi ...
- typora使用说明
一级标题 # 空格 编写内容 二级标题 ## 空格 编写内容 有序内容 1.+Tab 无序内容 -+Tab 代码块 print("hello wrold") typora快捷键 c ...
- MySQL事务。
相关资料:https://zhuanlan.zhihu.com/p/70701037 https://zhuanlan.zhihu.com/p/59061106 一.事务. 1.概念.事 ...
- Linux—网络防火墙详解
一.防火墙基本知识 二.firewall防火墙 # 安装firewalld [root@localhost ~]# apt-get install firewalld [root@localhost ...
- zabbix使用钉钉告警
1.钉钉创建群 2.[root@localhost ~]# vim /etc/zabbix/zabbix_server.conf # 配置文件中查找”Alert”查看告警脚本存放路径 [root@lo ...