【EF】EntityFramework DBFirst的使用

一、前言

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

 

二、添加EF       

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

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

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

         Step4:测试连接数据库;

         Step5:选择EF版本;

         Step6:选择要实体化的表,点击完成。

 

三、操作

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

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

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

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

        假设连接的数据库下有三个表: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; }
    }
    
}

        

         那么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; }

    }
}

 

           需要对表对象操作时,首先要:

private MyEntities dbContext = new MyEntities();

             需要对哪个表操作,就dbContext.ContractInfoes或者dbContext.CustomerInfoes这样找到数据集合,然后用Linq去操作,最后别忘dbContext.SaveChanges()保存修改即可。

 

四、结尾

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

转载于:https://www.cnblogs.com/lovecsharp094/p/7620466.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在.NET Core 6.0中使用Entity FrameworkEF)的Database First方式配置连接并进行实战是很简单的。 首先,确保你的目中已经安装了相应的NuGet包,包括`Microsoft.EntityFrameworkCore.Tools`和`Microsoft.EntityFrameworkCore.SqlServer`(如果你使用SQL Server作为数据库)。 接下来,打开终端或命令提示符窗口,进入你的目文件夹,并执行以下命令来生成EF的模型类: ``` dotnet ef dbcontext scaffold "YourConnectionString" Microsoft.EntityFrameworkCore.SqlServer -o Models ``` 在这个命令中,你需要将"YourConnectionString"替换为你的数据库连接字符串。该命令将自动生成EF的模型类,并将其放在`Models`文件夹中。 完成上述步骤后,你就可以在代码中使用这些生成的模型类来与数据库进行交互了。例如,你可以编写以下代码来获取数据库中的数据: ```csharp using System; using System.Linq; using Microsoft.EntityFrameworkCore; using YourProject.Models; // 替换为你生成的模型类所在的命名空间 namespace YourProject { class Program { static void Main(string[] args) { using (var context = new YourDbContext()) // 替换为你生成的DbContext类的名称 { var data = context.YourTableName.ToList(); // 替换为你数据库中的表名称 foreach (var item in data) { Console.WriteLine(item.PropertyName); // 替换为你表中的属性名称 } } } } } ``` 以上代码演示了如何通过使用生成的模型类和DbContext类来查询数据库中的数据。 这就是在.NET Core 6.0中使用EF的Database First方式配置连接并进行实战的简单过程。希望能对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值