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的更多相关文章

  1. IoC容器Autofac - Autofac + Asp.net MVC + EF Code First(转载)

    转载地址:http://www.cnblogs.com/JustRun1983/archive/2013/03/28/2981645.html  有修改 Autofac通过Controller默认构造 ...

  2. 解决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 ...

  3. MVC+EF CODE FIRST的使用

    1创建标准MVC项目 2通过NuGet安装EF 3在Models文件夹中编写实体类 4创建EFDB上下文类 5在webconfig中创建连接字符串,其中name=EFDB上下文类名 6通过管理控制台执 ...

  4. MVC, EF, Code First 相关问题总结

    1. 控制表名单复数: 在DbContext类中修改OnModelCreating()为: protected override void OnModelCreating(DbModelBuilder ...

  5. EF和MVC系列文章导航:EF Code First、DbContext、MVC

    对于之前一直使用webForm服务器控件.手写ado.net操作数据库的同学,突然来了EF和MVC,好多新概念泉涌而出,的确犹如当头一棒不知所措.本系列文章可以帮助新手入门并熟练使用EF和MVC,有了 ...

  6. MVC项目实践,在三层架构下实现SportsStore-01,EF Code First建模、DAL层等

    SportsStore是<精通ASP.NET MVC3框架(第三版)>中演示的MVC项目,在该项目中涵盖了MVC的众多方面,包括:使用DI容器.URL优化.导航.分页.购物车.订单.产品管 ...

  7. Contoso 大学 - 使用 EF Code First 创建 MVC 应用

    原文 Contoso 大学 - 使用 EF Code First 创建 MVC 应用 Contoso 大学 Web 示例应用演示了如何使用 EF 技术创建 ASP.NET MVC 应用.示例中的 Co ...

  8. 使用EF Code First搭建一个简易ASP.NET MVC网站,允许数据库迁移

    本篇使用EF Code First搭建一个简易ASP.NET MVC 4网站,并允许数据库迁移. 创建一个ASP.NET MVC 4 网站. 在Models文件夹内创建Person类. public ...

  9. MVC5中Model层开发数据注解 EF Code First Migrations数据库迁移 C# 常用对象的的修饰符 C# 静态构造函数 MSSQL2005数据库自动备份问题(到同一个局域网上的另一台电脑上) MVC 的HTTP请求

    MVC5中Model层开发数据注解   ASP.NET MVC5中Model层开发,使用的数据注解有三个作用: 数据映射(把Model层的类用EntityFramework映射成对应的表) 数据验证( ...

随机推荐

  1. Android JNI编程(六)——C语言函数指针、Unition联合体、枚举、Typedef别名、结构体、结构体指针

    版权声明:本文出自阿钟的博客,转载请注明出处:http://blog.csdn.net/a_zhon/. 目录(?)[+] 一:函数指针 1.函数指针顾名思义就是定义一个指针变量指向一个函数,和一级指 ...

  2. RandomStringUtils RandomUtils

    上一篇是StringUtils 链接http://www.cnblogs.com/tele-share/p/8060129.html 1.RandomStringUtils 1.1模拟实现random ...

  3. centos / Linux 服务环境下安装 Redis 5.0.3

    原文:centos / Linux 服务环境下安装 Redis 5.0.3 1.首先进入你要安装的目录 cd /usr/local 2.下载目前最新稳定版本 Redis 5.0.3 wget http ...

  4. 【hdu 3389】Game

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...

  5. android 连接USB按power键锁屏2声锁屏音

    alps\frameworks\base\packages\Keyguard\src\com\android\keyguard\KeyguardViewMediator.java #1384 行左右: ...

  6. 行列式(determinant)的物理意义及性质

    1. 物理(几何)意义 detA=output areainput area 首选,矩阵代表的是线性变换(linear transformation).上式说明一个矩阵的行列式(detA)几何意义上, ...

  7. 【BZOJ 1028】[JSOI2007]麻将

    [题目链接]:http://www.lydsy.com/JudgeOnline/problem.php?id=1028 [题意] [题解] /* 枚举新加入的一张牌是哪一张牌; 然后尝试把它加进去; ...

  8. centos7 firewall-cmd查看端口是否开放及开放端口

    查询端口号80 是否开启:firewall-cmd /tcp 永久开放80端口号:firewall-cmd --permanent --zone=public /tcp 移除80端口号:/tcp -- ...

  9. TCP可靠的传输机制

    TCP提供一种面向连接的.可靠的字节流服务.面向连接意味着两个使用TCP的应用(一般是一个客户和一个server)在彼此交换数据包之前必须先建立一个TCP连接.这一过程与打电话非常相似.先拨号振铃,等 ...

  10. struts2_11_实现自己的拦截器的定义

    1)登录界面代码: <% //设置session的值keyword为user request.getSession().setAttribute("user", " ...