博客
关于我
ASP.NET MVC5利用EF,反向自动生成数据库
阅读量:409 次
发布时间:2019-03-06

本文共 1546 字,大约阅读时间需要 5 分钟。

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
Movies { get; set; }21 }22 }
Movie

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

1  
配置文件,连接字符串

3.添加一个控制器,选择刚才我们创建的Model作为模型(即创建强类型视图)

4.这个时候,重新生成一下项目,就会在App_Data里面生成了一个数据库(Movie.mdf).

Entity Framework Code First detected that the database connection string that was provided pointed to a Moviesdatabase that didn’t exist yet, so Code First created the database automatically. 这句话的意思是:EF 代码先行检测到,数据库的连接字符串,指向了一个Movie的数据库,但是这个数据库并不存在,所以code first自动为我们创建了这个数据库。

5.You don't actually need to add the MovieDBContext connection string. If you don't specify a connection string, Entity Framework will create a LocalDB database in the users directory with the fully qualified name of the class (in this case MvcMovie.Models.MovieDBContext). You can name the database anything you like, as long as it has the .MDF  suffix. For example, we could name the database MyFilms.mdf.

这句话的大概意思是:你实际上不必添加我上面的字符串到webconifg文件中,因为EF会为我们按照用户项目的物理路径,创建一个全路径的名称的数据库。如果你添加了连接字符串,EF就会按照你写的,为你创建这个数据库。

6.EF为我们创建的数据库为:

 可以看出,EF为我们创建的数据库,string字段,默认是为空的。ID字段默认是主键。

转载地址:http://nxxkz.baihongyu.com/

你可能感兴趣的文章
MySQL binlog三种模式
查看>>
multi-angle cosine and sines
查看>>
Mysql Can't connect to MySQL server
查看>>
mysql case when 乱码_Mysql CASE WHEN 用法
查看>>
Multicast1
查看>>
mysql client library_MySQL数据库之zabbix3.x安装出现“configure: error: Not found mysqlclient library”的解决办法...
查看>>
MySQL Cluster 7.0.36 发布
查看>>
Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
查看>>
MySQL Cluster与MGR集群实战
查看>>
multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded
查看>>
mysql cmake 报错,MySQL云服务器应用及cmake报错解决办法
查看>>
Multiple websites on single instance of IIS
查看>>
mysql CONCAT()函数拼接有NULL
查看>>
multiprocessing.Manager 嵌套共享对象不适用于队列
查看>>
multiprocessing.pool.map 和带有两个参数的函数
查看>>
MYSQL CONCAT函数
查看>>
multiprocessing.Pool:map_async 和 imap 有什么区别?
查看>>
MySQL Connector/Net 句柄泄露
查看>>
multiprocessor(中)
查看>>
mysql CPU使用率过高的一次处理经历
查看>>