MVC EF Code First
1 在Models里面创建类,用[Key]特性指定主键;
2 在Model里面增加导航属性;
3 在web.config里面增加连接字符串
4 创建继承于DbContext的类
5 创建Controller类,生成Index视图
6 在Controller类的Index()里面,通过context.Database.CreateIfNotExist()
//BookInfo.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace CodeFirst.Models
{
public class BookInfo
{
[Key]
public int BookId { get; set; }
public string BookTitle { get; set; }
public int TypeId { get; set; }
public BookType BookType { get; set; }
}
}
//BookType.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace CodeFirst.Models
{
public class BookType
{
[Key]
public int TypeId { get; set; }
public string TypeTitle { get; set; }
public ICollection<BookInfo> BookInfo { get; set; }
}
}
//BooksContext
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace CodeFirst.Models
{
public class BooksContext:DbContext
{
public BooksContext():base("name=BooksContext")
{
}
DbSet<BookInfo> BookInfo { get; set; }
DbSet<BookType> BookType { get; set; }
}
}
//web.config
<connectionStrings>
<add name="BooksContext" connectionString="server=.;database=books;uid=sa;pwd=Server2012" providerName="System.Data.SqlClient"/>
</connectionStrings>
//BooksController
using CodeFirst.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace CodeFirst.Controllers
{
public class BooksController : Controller
{
DbContext context = new BooksContext();
//
// GET: /Books/
public ActionResult Index()
{
context.Database.CreateIfNotExists();
context.SaveChanges();
return View();
}
}
}
MVC EF Code First的更多相关文章
- IoC容器Autofac - Autofac + Asp.net MVC + EF Code First(转载)
转载地址:http://www.cnblogs.com/JustRun1983/archive/2013/03/28/2981645.html 有修改 Autofac通过Controller默认构造 ...
- 解决MVC EF Code First错误:Model compatibility cannot be checked because the EdmMetadata type was not included in the model.
Model compatibility cannot be checked because the EdmMetadata type was not included in the model. En ...
- MVC+EF CODE FIRST的使用
1创建标准MVC项目 2通过NuGet安装EF 3在Models文件夹中编写实体类 4创建EFDB上下文类 5在webconfig中创建连接字符串,其中name=EFDB上下文类名 6通过管理控制台执 ...
- MVC, EF, Code First 相关问题总结
1. 控制表名单复数: 在DbContext类中修改OnModelCreating()为: protected override void OnModelCreating(DbModelBuilder ...
- EF和MVC系列文章导航:EF Code First、DbContext、MVC
对于之前一直使用webForm服务器控件.手写ado.net操作数据库的同学,突然来了EF和MVC,好多新概念泉涌而出,的确犹如当头一棒不知所措.本系列文章可以帮助新手入门并熟练使用EF和MVC,有了 ...
- MVC项目实践,在三层架构下实现SportsStore-01,EF Code First建模、DAL层等
SportsStore是<精通ASP.NET MVC3框架(第三版)>中演示的MVC项目,在该项目中涵盖了MVC的众多方面,包括:使用DI容器.URL优化.导航.分页.购物车.订单.产品管 ...
- Contoso 大学 - 使用 EF Code First 创建 MVC 应用
原文 Contoso 大学 - 使用 EF Code First 创建 MVC 应用 Contoso 大学 Web 示例应用演示了如何使用 EF 技术创建 ASP.NET MVC 应用.示例中的 Co ...
- 使用EF Code First搭建一个简易ASP.NET MVC网站,允许数据库迁移
本篇使用EF Code First搭建一个简易ASP.NET MVC 4网站,并允许数据库迁移. 创建一个ASP.NET MVC 4 网站. 在Models文件夹内创建Person类. public ...
- MVC5中Model层开发数据注解 EF Code First Migrations数据库迁移 C# 常用对象的的修饰符 C# 静态构造函数 MSSQL2005数据库自动备份问题(到同一个局域网上的另一台电脑上) MVC 的HTTP请求
MVC5中Model层开发数据注解 ASP.NET MVC5中Model层开发,使用的数据注解有三个作用: 数据映射(把Model层的类用EntityFramework映射成对应的表) 数据验证( ...
随机推荐
- (三)Unity5.0新特性------动画的StateMachineBehaviours
出处:http://blog.csdn.net/u010019717 author:孙广东 时间:2015.3.31 (State machine behaviours)状态机的行为在Ani ...
- [Vue] Create Filters in Vue.js
Just like in the command line, you can pipe a property through a filter to get a desired result. You ...
- 开源server软件
Java缓存server jmemcached http://www.oschina.net/p/jmemcached jmemcached 是一个Java版的 memcached 缓存server, ...
- [React] Use React Context to Manage Application State Through Routes
We’ll create a Router component that will wrap our application and manage all URL related state. We’ ...
- Thread和ThreadPool
Thread和ThreadPool 说到多线程异步编程,总会说起Thread.ThreadPool.Task.TPL这一系列的技术.总结整理了一版编程示例和实践,分享给大家. 先从Thread和Thr ...
- Android菜鸟的成长笔记(21)——跨进程调用Service
我们都知道在Android中的每一个应用是一个进程,其实每一个应用就相当于Linux系统里面的一个用户,进程和进程之间的通信其实就相当于用户和用户之间的通信,为了实现这种跨进程通信,Android提供 ...
- UnitOfWork
数据访问层之UnitOfWork 接上文 项目架构开发:数据访问层之IQuery 本章我们继续IUnitOfWork的开发,从之前的IRepository接口中就可以看出,我们并没有处理单元事务, ...
- jQuery迭代器
http://www.imooc.com/code/3417 迭代器 迭代器是一个框架的重要设计.我们经常需要提供一种方法顺序用来处理聚合对象中各个元素,而又不暴露该对象的内部,这也是设计模式中的迭代 ...
- backbone Model
requirejs.config({ baseUrl: 'js/lib', paths:{ app: '../app' } }) // Start the main app logic. //requ ...
- JSON格式的服务接口
电商接口 京东获取单个商品价格接口: http://p.3.cn/prices/mgets?skuIds=J_商品ID&type=1 用例 ps:商品ID这么获取:http://item.jd ...