1)、新建ADO.NET实体数据模型--->选择空CodeFirst模型
2)、新建两个实体类(客户表和订单信息表)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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(100,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; } } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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方法
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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中
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
DataModel db = new DataModel(); Customer cust = new Customer { Address = "江西省南昌市瑶湖区", CustomerAge = 19, CustomerName = "张三", CustomerSex = "男", Email = "999999999@qq.com", Phone = "12345678910" }; 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就可以了。