自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

MVC使用EntityFramework(EF)生成數(shù)據(jù)庫模型

運維 數(shù)據(jù)庫運維
本文系列將向你介紹使用EntityFramework(EF)生成數(shù)據(jù)庫模型。文章會一步一步的教您使用Entity Framewok4創(chuàng)建數(shù)據(jù)庫。

首先打開VS2013,新建Web項目mcc,使用MVC模板。

右擊引用,管理NuGet程序包,安裝EntityFramework。

在Model文件下新建類Employee,新增幾個屬性,比如:EmployeeId,F(xiàn)irstName,LastName,Salary。

  1. public int EmployeeId { get; set; } 
  2. public string FirstName { get; set; } 
  3. public string LastName { get; set; } 
  4. public int Salary { get; set; } 

引用using System.ComponentModel.DataAnnotations; 將EmployeeId 設(shè)置為主鍵。

在Web.Config里面設(shè)置數(shù)據(jù)庫連接字符串

<add name="MyDBConnectString" providerName="System.Data.SqlClient" connectionString="Data Source=.;Initial Catalog=SalesERPDAL;user id=sa;password=sa"/>

在根目錄下新建文件夾DataAccessLayer,新建類SalesERPDAL,繼承DbContext。

在 CodeFirst 模式,根據(jù)實體類生成對應(yīng)數(shù)據(jù)庫表。

  1. public class SalesERPDAL : DbContext 
  2.     { 
  3.         public SalesERPDAL() : base("MyDBConnectString")//數(shù)據(jù)庫連接字符串 
  4.         { 
  5.             this.Configuration.ProxyCreationEnabled = true
  6.             var aaa = new DbMigrationsConfiguration();//設(shè)置自動遷移屬性 
  7.             aaa.AutomaticMigrationsEnabled = true
  8.         } 
  9.         protected override void OnModelCreating(DbModelBuilder modelBuilder) 
  10.         { 
  11.             modelBuilder.Entity<Employee>().ToTable("TblEmployee");//設(shè)置生成對應(yīng)數(shù)據(jù)庫表的名稱 
  12.             base.OnModelCreating(modelBuilder); 
  13.         } 
  14.   
  15.         public DbSet<Employee> Employees { get; set; } 
  16.     } 

此時,基本設(shè)置完成,開始使用命令創(chuàng)建數(shù)據(jù)庫,生成表。

打開工具-NuGet程序包管理器-程序包管理器控制臺

輸入命令:Enable-Migrations ,允許遷移。

輸入命令:Enable-Migrations -ContextTypeName aaa.DataAccessLayer.SalesERPDAL,指定遷移類型。

輸入命令:Add-Migration ,將掛起的模型更改寫入基于代碼的遷移。

Name:update(隨意輸入)

輸入命令: Update-Database -Verbose,執(zhí)行生成命令,創(chuàng)建數(shù)據(jù)庫,更新表。

如上圖,已經(jīng)可以在數(shù)據(jù)庫中查看到對應(yīng)的表,可以插入數(shù)據(jù),進行獲取驗證了。

責(zé)任編輯:武曉燕 來源: 博客園
相關(guān)推薦

2023-02-23 07:46:48

學(xué)習(xí)模型數(shù)據(jù)倉庫

2010-05-07 14:29:45

Unix--Tripw

2021-05-10 09:22:44

.NET數(shù)據(jù)庫項目

2025-03-05 08:40:43

項目數(shù)據(jù)庫流程

2020-08-06 11:45:37

數(shù)據(jù)庫文檔Swagger

2009-12-30 14:12:53

ADO.NET Fra

2024-08-13 10:36:25

SpringScrew數(shù)據(jù)庫

2011-03-23 09:54:47

數(shù)據(jù)模型數(shù)據(jù)庫設(shè)計

2009-12-31 09:18:23

ADO.NET對象模型

2025-01-03 08:13:08

2020-12-24 10:20:43

文檔工具語言

2009-05-14 14:23:25

微軟ado.netLINQ

2011-03-04 09:40:42

AJAX開發(fā)集成數(shù)據(jù)庫

2010-06-17 12:59:07

Oracle

2016-08-16 09:06:07

Entity FramT4模板sql語句

2022-07-12 10:48:27

Python數(shù)據(jù)日報命令

2024-12-04 14:56:10

2024-12-23 11:31:05

大模型檢索人工智能

2024-03-20 08:37:34

數(shù)據(jù)處理Python數(shù)據(jù)分析

2023-11-06 06:39:36

數(shù)據(jù)分析Python
點贊
收藏

51CTO技術(shù)棧公眾號