EntityFramework 使用流程

EntityFramework是一种。net对象和数据库对象关系映射程序(ORMhtml

一.EF Core EF6sql

1.运行环境数据库

(1)EF6:使用.netFramework包,只能运行在Windows平台上ide

(2)EFCore:使用最新的EntityFramework Core包,支持跨平台ui

2.功能比较spa

(1)EFCore中新增了一些新的功能,同时也缺乏一些EF6的功能.net

二.同时使用EF6EFCorecode

1.经过命名空间别名指令轻松消除多义性htm

2.using Microsoft.EntityFrameworkCore; // use DbContext for EF Core 对象

3.using EF6 = System.Data.Entity; // use EF6.DbContext for the EF6 version

EF使用流程

1.建立项目引入EF6

2.建立XXXContext,继承DBContext

public class SchoolContext : DbContext
    {
        public SchoolContext() : base("name=SchoolDBName")//配置文件中链接数据库语句的Name
        {

        }
        public DbSet<SchoolInfo> SchoolInfo { get; set; }
        public DbSet<Student> Student { get; set; }
    }

  3.建立实体模型

public class SchoolInfo
    {
        [Key]
        public Guid Guid { get; set; }
        public string SchoolName { get; set; }
        public string SchoolUrl { get; set; }
        public string Address { get; set; }
        public DateTime CreateSchool { get; set; }
        public string Phone { get; set; }
        public bool state { get; set; }
    }
 public class Student
    {
        [Key]
        public Guid Guid { get; set; }
        public string Name { get; set; }
        public Guid SchoolGuid { get; set; }
        public bool Sex { get; set; }
    }

  4.设置链接的数据库

<connectionStrings>
    <add name="SchoolDBName" connectionString="Data Source=.;database=SchoolDB;uid=sa;pwd=sasa;" providerName="System.Data.SqlClient" />
  </connectionStrings>

  5.填写执行方法

static void Main(string[] args)
        {
            SchoolInfo schoolInfo = new SchoolInfo()
            {
                Guid = System.Guid.NewGuid(),
                SchoolName = "SchoolName",
                SchoolUrl = "SchoolUrl",
                Address = "Address",
                CreateSchool = DateTime.Now,
                Phone = "123456789",
                state = true,
            };
            if (new SchoolInfoBLL().Add(schoolInfo))
            {
                Console.WriteLine("成功");

            }
            else {
                Console.WriteLine("失败");
            }
            Console.ReadKey();
        }

  若是类属性发生变化

1.执行 enable-migrations -contexttypename SchoolContext

2.修改configuration.cs

internal sealed class Configuration : DbMigrationsConfiguration<ConsoleApp4.Model.DataBase.SchoolContext>
    {
        public Configuration()
        {
            //开启自动迁移
            AutomaticMigrationsEnabled = true;
            ContextKey = "ConsoleApp4.Model.DataBase.SchoolContext";
        }

        protected override void Seed(ConsoleApp4.Model.DataBase.SchoolContext context)
        {
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data.
        }
    }

 3.执行add-migration InitialCreate

    4.执行 update-database 

  注:以后的完属性直接执行第4部便可

注意:当EF须要执行大批量数据时,能够先生成完整的sql语句,而后直接执行sql语句

var sql = new StringBuilder();
                  for (int i = 0; i < 10000; i++)
                  {
                     //生成SQL
                     sql.Append(ItemDetailBatchs.BatchAdd(entity));
                 }
                 //一次性执行SQL
              db.Database.ExecuteSqlCommand(sql.ToString());

  

 

原文出处:https://www.cnblogs.com/JueXiaoQiang/p/10385152.html

相关文章
相关标签/搜索