国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

ASP.NET MVC5利用EF,反向自動生成數據庫

2019-11-17 02:10:14
字體:
來源:轉載
供稿:網友

asp.net MVC5利用EF,反向自動生成數據庫

1.在Model類里面,寫好相應的屬性。

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Data.Entity; 6  7 namespace MvcMovie.Models 8 { 9     public class Movie10     {11         public int ID { get; set; }12         public string Title { get; set; }13         public DateTime ReleaseDate { get; set; }14         public string Genre { get; set; }15         public decimal PRice { get; set; }16     }17 18     public class MovieDBContext : DbContext19     {20         public DbSet<Movie> Movies { get; set; }21     }22 }
Movie

2.在配置文件中,寫上:

1  <add name="MovieDBContext"2    connectionString="Data Source=(LocalDB)/v11.0;AttachDbFilename=|DataDirectory|/Movies.mdf;Integrated Security=True"3    providerName="System.Data.SqlClient"/>
配置文件,連接字符串

3.添加一個控制器,選擇剛才我們創建的Model作為模型(即創建強類型視圖)

4.這個時候,重新生成一下項目,就會在App_Data里面生成了一個數據庫(Movie.mdf).

Entity Framework Code First detected that the database connection string thatwas provided pointed to aMoviesdatabase that didn’t exist yet, soCode First created the database automatically. 這句話的意思是:EF 代碼先行檢測到,數據庫的連接字符串,指向了一個Movie的數據庫,但是這個數據庫并不存在,所以code first自動為我們創建了這個數據庫。

5.You don't actually need to add theMovieDBContextconnection string. If you don't specify aconnection string, Entity Framework will create a LocalDB database in the usersdirectory with the fully qualified name of theDbContextclass (in this caseMvcMovie.Models.MovieDBContext). You can name the database anything you like, as long as it has the.MDF suffix. For example, we could name the databaseMyFilms.mdf.

這句話的大概意思是:你實際上不必添加我上面的字符串到webconifg文件中,因為EF會為我們按照用戶項目的物理路徑,創建一個全路徑的名稱的數據庫。如果你添加了連接字符串,EF就會按照你寫的,為你創建這個數據庫。

6.EF為我們創建的數據庫為:

可以看出,EF為我們創建的數據庫,string字段,默認是為空的。ID字段默認是主鍵。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 桐柏县| 满城县| 怀来县| 洛南县| 渭南市| 都匀市| 华坪县| 大悟县| 马山县| 遂川县| 淮南市| 定远县| 沛县| 房山区| 上高县| 怀柔区| 靖边县| 出国| 淮阳县| 恩施市| 西丰县| 五家渠市| 邢台县| 昭平县| 阳原县| 阳曲县| 阳春市| 屏东市| 慈利县| 峨山| 鸡西市| 迁安市| 平遥县| 霍林郭勒市| 德庆县| 湖南省| 中宁县| 石楼县| 旌德县| 漳浦县| 繁昌县|