ASP.NET MVC5利用EF,反向自动生成数据库
2015-06-20 09:35
706 查看
1.在Model类里面,写好相应的属性。
配置文件,连接字符串
3.添加一个控制器,选择刚才我们创建的Model作为模型(即创建强类型视图)
4.这个时候,重新生成一下项目,就会在App_Data里面生成了一个数据库(Movie.mdf).
Entity Framework Code First detected that the database connection string that was provided pointed to a
5.You don't actually need to add the
这句话的大概意思是:你实际上不必添加我上面的字符串到webconifg文件中,因为EF会为我们按照用户项目的物理路径,创建一个全路径的名称的数据库。如果你添加了连接字符串,EF就会按照你写的,为你创建这个数据库。
6.EF为我们创建的数据库为:
可以看出,EF为我们创建的数据库,string字段,默认是为空的。ID字段默认是主键。
<add name="MovieDBContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>
配置文件,连接字符串
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
MovieDBContextconnection 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 DbContextclass (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字段默认是主键。
相关文章推荐
- 基于Raspberry Pi Raspbian 的 Hadoop 安装
- ASP.NET性能优化之让浏览器缓存动态网页
- ASP.NET MVC 开启AJAX跨域请求
- Asp.NET的目的是学习
- 【ASP.NET】初次见面
- 17.AspxGridView服务器事件列表
- popupWin 属性及用法介绍 ASP.NET控件,仿QQ,msn右下角弹出窗口
- ASP.NET MVC中将控制器分离到类库的实现(转)
- ASP.NET WebAPI RC 竟然不支持最常用的json传参
- 适合ASP.NET Web API使用的场景
- 改善ASP.NET Web API性能的八项技术
- ASP.Net增删查改写的一个通讯录系统---ShinePans
- ASP.NET Web API 用IE浏览器访问下载文件问题
- ASP.NET WebApi 文件上传功能
- Asp.net MVC Session过期异常的处理
- ASP.NET MVC中的Session设置
- web渗透(asp数字型注入模拟实战)
- Nancy和ASP.NET MVC的简单对比
- ListView 使用方法(Asp.Net)
- UIView的contentMode中scale和aspect的字面意思