【EF】EntityFramework DBFirst的采取爱博体育app

一、前言

一、前言

       久闻EF大名,在此之前做C/S产品用的是Dapper对SqlLite进行O奥迪Q3M。然后接触公司授权系统后意识用的是EntityFramework对SQLSever实行OCR-VM。授权系统里用的是DBFirst,增加和删除查改使用Linq
To Entity,感到尤其便利。本篇篇幅非常的短,老车手可一贯略过

       久闻EF大名,以前做C/S产品用的是Dapper对SqlLite实行O卡宴M。然后接触集团授权系统后发觉用的是EntityFramework对SQLSever举办O奇骏M。授权系统里用的是DBFirst,增加和删除查改使用Linq
To Entity,感觉这多少个便于。本篇篇幅十分的短,老驾车员可直接略过

 

 

二、添加EF       

二、添加EF       

        Step一:加多“新建项”,起个名称,增加ADO.NET实体数据模型;

        Step一:加多“新建项”,起个名称,加多ADO.NET实体数据模型;

        
Step2:接纳模型类型,来自数据库的EF设计器;

        
Step贰:选拔模型类型,来自数据库的EF设计器;

         Step三:选用数据连接,新建连接,选用要选择的数据库类型;默许SQLSever;

         Step3:采用数据连接,新建连接,选用要运用的数据库类型;暗中同意SQLSever;

        
Step4:测试连接数据库;

        
Step肆:测试连接数据库;

        
Step5:选择EF版本;

        
Step5:选择EF版本;

        
Step6:选取要实体化的表,点击落成。

        
Step六:选用要实体化的表,点击完结。

 

 

三、操作

三、操作

       经过上述操作会发生八个公文:

       经过上述操作会产生八个公文:

        1.
EntityModel.Context.tt
(上下文,全数class的DBSet集结都在那一个文件下的.cs文件中)

        壹.
EntityModel.Context.tt
(上下文,全数class的DBSet集合都在那些文件下的.cs文件中)

        二. EntityModel.tt      
(每一种表映射后的class都位于那些文件上边)

        贰. EntityModel.tt      
(每一种表映射后的class都位居那些文件上面)

        3. EntityModel.edmx
(可视化的表设计器)

        三. EntityModel.edmx
(可视化的表设计器)

       
要是连接的数据库下有多个表:AgencyInfo,ContractInfo,CustomerInfo。那么EntityModel.tt下就会有两个关照的.cs文件:

       
假诺连接的数据库下有八个表:AgencyInfo,ContractInfo,CustomerInfo。那么EntityModel.tt下就会有八个料理的.cs文件:

//------------------------------------------------------------------------------
// <auto-generated>
//    This code was generated from a template.
//
//    Manual changes to this file may cause unexpected behavior in your application.
//    Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;

namespace HHH
{
    public partial class AgencyInfo
    {
        public System.Guid ID { get; set; }
        public string UnitName { get; set; }
        public string Phone { get; set; }
        public string Address { get; set; }
        public string comments { get; set; }
        public Nullable<System.DateTime> CreatTime { get; set; }
        public Nullable<int> ShowFlag { get; set; }
    }

}

//------------------------------------------------------------------------------
// <auto-generated>
//    This code was generated from a template.
//
//    Manual changes to this file may cause unexpected behavior in your application.
//    Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;

namespace HHH
{
    public partial class ContractInfo
    {
        public System.Guid ID { get; set; }
        public string Title { get; set; }
        public string Comment { get; set; }
        public Nullable<System.DateTime> CreateDate { get; set; }
    }

}

//------------------------------------------------------------------------------
// <auto-generated>
//    This code was generated from a template.
//
//    Manual changes to this file may cause unexpected behavior in your application.
//    Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;

namespace HHH
{
    public partial class CustomerInfo
    {
        public System.Guid ID { get; set; }
        public string Name { get; set; }
        public string ContactInfo { get; set; }
        public string Address { get; set; }
        public string Comments { get; set; }
        public string Email { get; set; }
        public string MobilePhone { get; set; }
        public string province { get; set; }
        public string City { get; set; }
        public string Type { get; set; }
        public Nullable<System.DateTime> CreateDate { get; set; }
    }

}
//------------------------------------------------------------------------------
// <auto-generated>
//    This code was generated from a template.
//
//    Manual changes to this file may cause unexpected behavior in your application.
//    Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;

namespace HHH
{
    public partial class AgencyInfo
    {
        public System.Guid ID { get; set; }
        public string UnitName { get; set; }
        public string Phone { get; set; }
        public string Address { get; set; }
        public string comments { get; set; }
        public Nullable<System.DateTime> CreatTime { get; set; }
        public Nullable<int> ShowFlag { get; set; }
    }

}

//------------------------------------------------------------------------------
// <auto-generated>
//    This code was generated from a template.
//
//    Manual changes to this file may cause unexpected behavior in your application.
//    Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;

namespace HHH
{
    public partial class ContractInfo
    {
        public System.Guid ID { get; set; }
        public string Title { get; set; }
        public string Comment { get; set; }
        public Nullable<System.DateTime> CreateDate { get; set; }
    }

}

//------------------------------------------------------------------------------
// <auto-generated>
//    This code was generated from a template.
//
//    Manual changes to this file may cause unexpected behavior in your application.
//    Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;

namespace HHH
{
    public partial class CustomerInfo
    {
        public System.Guid ID { get; set; }
        public string Name { get; set; }
        public string ContactInfo { get; set; }
        public string Address { get; set; }
        public string Comments { get; set; }
        public string Email { get; set; }
        public string MobilePhone { get; set; }
        public string province { get; set; }
        public string City { get; set; }
        public string Type { get; set; }
        public Nullable<System.DateTime> CreateDate { get; set; }
    }

}

        

        

        
那么EntityModel.Context.tt是如此的:

        
那么EntityModel.Context.tt是那样的:

//------------------------------------------------------------------------------
// <auto-generated>
//    This code was generated from a template.
//
//    Manual changes to this file may cause unexpected behavior in your application.
//    Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;

namespace HHH
{
    public partial class MyEntities: DbContext
    {
        public MyEntities()
            : base("name=MyEntities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public DbSet<ContractInfo> ContractInfoes { get; set; }
        public DbSet<CustomerInfo> CustomerInfoes { get; set; }
        public DbSet<AgencyInfo> AgencyInfoes { get; set; }

    }
}
//------------------------------------------------------------------------------
// <auto-generated>
//    This code was generated from a template.
//
//    Manual changes to this file may cause unexpected behavior in your application.
//    Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;

namespace HHH
{
    public partial class MyEntities: DbContext
    {
        public MyEntities()
            : base("name=MyEntities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public DbSet<ContractInfo> ContractInfoes { get; set; }
        public DbSet<CustomerInfo> CustomerInfoes { get; set; }
        public DbSet<AgencyInfo> AgencyInfoes { get; set; }

    }
}

 

 

          
须求对表对象操作时,首先要:

          
须求对表对象操作时,首先要:

private MyEntities dbContext = new MyEntities();
private MyEntities dbContext = new MyEntities();

             必要对哪些表操作,就dbContext.ContractInfoes大概dbContext.CustomerInfoes那样找到数据集结,然后用Linq去操作,最终别忘dbContext.SaveChanges()保存修改就可以。

             需求对哪些表操作,就dbContext.ContractInfoes可能dbContext.CustomerInfoes那样找到数据会集,然后用Linq去操作,最后别忘dbContext.SaveChanges()保存修改就可以。

 

 

四、结尾

四、结尾

       后天再看看写点什么,保持学习进程,再过几天要去练车了

       前几天再看看写点什么,保持学习进程,再过几天要去练车了

相关文章