zoukankan      html  css  js  c++  java
  • 2.第一个Codefirst实例

    1.什么是codefirst

    EF4.1中开始支持Code First 。这种方式在领域设计模式中非常有用。使用Code First模式,你可以专注于领域设计,根据需要,为你一个领域的对象创建类集合,而不是首先来设计数据库,然后来根据数据库设计你的类,Code-First APis将会基于你的类和配置,为你创建数据库

    2.我的第一个codefirst实例

      1)添加NuGet程序包EF(直接输入EF即可)

      2)创建一个类 EFDbContext 并继承与 DbContext,该类派生自System.Data.Entity。DbContext类,如下所示。派生DbContext的类在实体框架中称为context类。

      3)在model中创建实体类(用于映射到数据库)

      4)在EFDbContext 中定义要创建上下文的模拟生成器(通过对生成数据库时的一些约定)

      5)在EFDbContext 中定义DbSet集合(这些集合在创建派生类的实例时自动初始化)

      6)在web.config中添加连接数据库的字符串

      7)创建控制器,并调用上下文,运行(运行后可以在连接字符串所指定地址的数据库查看创建的数据库)

    代码如下:

      EFDbContext :

    using Entitys;
    using System.Data.Entity;
    using System.Data.Entity.ModelConfiguration.Conventions;
    
    namespace CodeFirst2.Context
    {
        public class EFDbContext : DbContext
        {
            //调用父类的一个参数的构造函数初始化实例
            //指定数据库,如果不指定,会在项目"App_Data"中自动生成
            public EFDbContext() : base("EFDbContext") { }
    
            /// <summary>
            /// 生成数据库
            /// 通常,在创建派生上下文的第一个实例时仅调用此方法一次。 
            /// 然后将缓存该上下文的模型,并且该模型适用于应用程序域中的上下文的所有后续实例。 
            /// 通过在给定的 ModelBuidler 上设置 ModelCaching 属性可禁用此缓存,但注意这样做会大大降低性能。 
            /// 通过直接使用 DbModelBuilder 和 DbContextFactory 类来提供对缓存的更多控制。
            /// </summary>
            /// <param name="modelBuilder">定义要创建的上下文的模型的生成器。</param>
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                //去除“设置表明为复数的约定”
                modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            }
    
            //这些集合在创建派生类的实例时自动初始化
            public DbSet<People> Peoples { get; set; }
            public DbSet<PeoType> PeoTypes { get; set; }
        }
    }
    View Code

      实体代码:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    
    namespace Entitys
    {
        public class People
        {
            [Key]//主键
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]//  在插入行时,数据库将生成值
            public Guid Id { get; set; }
            public string Name { get; set; }
            public string Age { get; set; }
            public DateTime CreateDate { get; set; }
            public int Type { get; set; }
    
            public virtual ICollection<PeoType> PeoTypes { get; set; } = new List<PeoType>();
        }
    }
    View Code
    using System;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    
    namespace Entitys
    {
        public class PeoType
        {
            [Key]
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
            public Guid Id { get; set; }
            public string Name { get; set; }
        }
    }
    View Code

       控制器:

    using CodeFirst2.Context;
    using System.Web.Mvc;
    
    namespace CodeFirst2.Controllers
    {
        public class DefaultController : Controller
        {
            // GET: Default
            public ActionResult Index()
            {
                EFDbContext db = new EFDbContext();
                return View();
            }
        }
    }
    View Code

       web.config:

    <connectionStrings>
        <add name="EFDbContext" connectionString="server=.;uid=sa;pwd=svse;database=EFDemo;" providerName="System.Data.SqlClient" />
      </connectionStrings>
    View Code

     先实现第一个实例,后面将逐个讲解

  • 相关阅读:
    5.我国最高山峰是珠穆朗玛峰,8848米。现在我有一张足够大的纸,它的厚度是0.01米。请问,我折叠多少次,可以折成珠穆朗玛峰的高度。
    sqylog 50道练习题
    sqylog练习题 2018.7.10
    WPF---依赖属性(一)
    C#基础知识---is与as
    C#基础知识---装箱与拆箱
    C#基础知识---Lambda表达式
    C#基础知识---Linq操作XML文件
    C#基础知识---匿名方法使用
    C#基础知识---?为何物
  • 原文地址:https://www.cnblogs.com/zjdbk/p/10519350.html
Copyright © 2011-2022 走看看