【EF】EntityFramework DBFirst的施用。【EF】EntityFramework DBFirst的运用。

一、前言

一、前言

       久闻EF大名,之前举行C/S产品从而的是Dapper对SqlLite进行ORM。然后接触公司授权系统后意识用底凡EntityFramework对SQLSever进行ORM。授权系统里之所以之是DBFirst,增删查改使用Linq
To Entity,觉得那个有利。本篇篇幅较短,老驾驶员可直接略过

       久闻EF大名,之前做C/S产品从而底凡Dapper对SqlLite进行ORM。然后接触公司授权系统后发觉用之凡EntityFramework对SQLSever进行ORM。授权系统里之所以底凡DBFirst,增删查改使用Linq
To Entity,觉得非常方便。本篇篇幅较短,老车手可一直略过

 

 

二、添加EF       

二、添加EF       

        Step1:添加“新建起”,起个号,添加ADO.NET实体数据模型;

        Step1:添加“新建起”,起个称呼,添加ADO.NET实体数据模型;

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

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

         Step3:选择数据连接,新建连接,选择而动的数据库类型;默认SQLSever;

         Step3:选择数据连接,新建连接,选择而利用的数据库类型;默认SQLSever;

        
Step4:测试连接数据库;

        
Step4:测试连接数据库;

        
Step5:选择EF版本;

        
Step5:选择EF版本;

        
Step6:选择而实体化的申,点击完成。

        
Step6:选择而实体化的阐发,点击完成。

 

 

三、操作

三、操作

       经过上述操作会产生三只文本:

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

        1.
EntityModel.Context.tt
(上下文,所有class的DBSet集合都当此文件下的.cs文件中)

        1.
EntityModel.Context.tt
(上下文,所有class的DBSet集合都于这文件下的.cs文件被)

        2. EntityModel.tt      
(每个表映射后底class都位于这文件下面)

        2. EntityModel.tt      
(每个表映射后的class都居这文件下面)

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

        3. 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()保存修改即可。

 

 

四、结尾

四、结尾

       明天复看写点啊,保持学习进度,再过几天而错过练车了

       明天再也看看写点什么,保持学习进度,再过几上而失去练车了

相关文章

admin

网站地图xml地图