• (3).Net Core的EF Core的使用


    官方文档:https://docs.microsoft.com/zh-cn/ef/core/index

    Nuget安装:

    Install-package Microsoft.EntityFrameworkCore
    Install-package Microsoft.EntityFrameworkCore.SqlServer
    Micorsoft.EntityFrameworkCore:EF框架的核心包
    Micorsoft.EntityFrameworkCore.SqlServer:针对SqlServer数据库的扩展,使用SqlServer数据库必须。类似的还有MySql,SqlLite等
    Micorsoft.EntityFrameworkCore.Tools
    &Micorosft.EntityFrameworkCore.Design:用户根据现有的数据库生成模型代码,CodeFirst模式不需要安装后两个。

    模型创建以及配置:

    • 建立模型实体类,实现的是一个权限管理,建立以下表
    public class UserEntity :BaseEntity
        {
            public long ID { get; set; }
            public string Name { get; set; }
            public string PhoneNum { get; set; }
            public string Email { get; set; }
            public string PWDSalt { get; set; }
            public string PWDHash { get; set; }
            public int LoginErrorTimes { get; set; }
            public DateTime? LastLoginErrorTime { get; set; }
        }
     public class RoleEntity : BaseEntity
        {
            public long ID { get; set; }
            public string Name { get; set; }
        }
    
    public class PermissionEntity:BaseEntity
        {
            public long ID { get; set; }
            public string Name { get; set; }
            public string Description { get; set; }
        }
    
    •  实体配置:

    EF Core和EF差不多,都有一些默认配置

    bool  ---》bit    1(长度) 

    string ---> longtext              可空

    DateTime --->datetime 6  

    .........等等,下面举例说明常用到的几个api

    var modelBuilder=new ModelBuilder();
    var userBuilder= modelBuilderEntity<UserEntity>().ToTable("T_Users");//映射到哪个表
       userBuilder.Property(u => u.Name).HasMaxLength(50).IsRequired(true);//设置长度与是否可空,调用IsRequired默认为True不可空
    
     userRoleRelation.HasOne(m => m.User).WithMany().HasForeignKey(m => m.UserID).IsRequired();//一对多关系配置

    数据库上下文配置

    • 配置数据库上下文,建立自己的MyDbContext(名字随意),继承DbContext,重写OnModelCreating(配置模型类到数据库表字段映射)和OnConfiguring(通过DbContextOptionsBuilder配置数据库连接等)方法;
    • 由于EF Core到了2.0还没有支持多对多关系的配置,所以我们要通过两个一对多关系的配置去实现:
     public class MyDbContext : DbContext
        {
            public DbSet<UserEntity> Users { get; set; }
            public DbSet<PermissionEntity> Permissions { get; set; }
            public DbSet<RoleEntity> Roles { get; set; }
            protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                
                base.OnModelCreating(modelBuilder);
                var userBuilder= modelBuilder.Entity<UserEntity>().ToTable("T_Users");
                userBuilder.Property(u => u.Email).HasMaxLength(50);
                userBuilder.Property(u => u.Name).HasMaxLength(50).IsRequired();
                userBuilder.Property(u => u.PhoneNum).HasMaxLength(20).IsRequired();
    
                var permissionBuilder= modelBuilder.Entity<PermissionEntity>().ToTable("T_Permissions");
                permissionBuilder.Property(p => p.Name).IsRequired().HasMaxLength(50);
                permissionBuilder.Property(p => p.Description).HasMaxLength(50);
    
    
                modelBuilder.Entity<RoleEntity>().ToTable("T_Roles");
    
                var userRoleRelation = modelBuilder.Entity<UserRoleEntity>().ToTable("T_UserRoleRelation");
                userRoleRelation.HasOne(m => m.User).WithMany().HasForeignKey(m => m.UserID).IsRequired();
                userRoleRelation.HasOne(m => m.Role).WithMany().HasForeignKey(m => m.RoleID).IsRequired();
    
    
                var rolePermissionRelation = modelBuilder.Entity<RolePermissionEntity>().ToTable("T_RolePermissionRelation");
                rolePermissionRelation.HasOne(m => m.Role).WithMany().HasForeignKey(m => m.RoleID).IsRequired();
                rolePermissionRelation.HasOne(m => m.Permission).WithMany().HasForeignKey(m => m.PermissionID).IsRequired();
            }
    
            protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            {
                base.OnConfiguring(optionsBuilder);
                optionsBuilder.UseMySql(ConfigHelper.SqlConnStr());
            }
    
        }

     

    数据库迁移

    应用数据库迁移,根据模型类生成数据库

    • nuget安装install-package Microsoft.EntityFrameworkCore.Tools包,用于数据库迁移

    • 然后Add-Migration initDB,初始化数据库
    • 最后Update-Database即可生成数据库了:

  • 相关阅读:
    Java知多少(57)主线程
    Java知多少(56)线程模型
    Java知多少(55)线程
    Java知多少(54)断言详解
    Java知多少(53)使用Java创建自己的异常子类
    Java知多少(52)内置异常
    Java知多少(51)finally
    Java知多少(50)Java throws子句
    Java知多少(49)throw:异常的抛出
    Java知多少(48)try语句的嵌套
  • 原文地址:https://www.cnblogs.com/lyfingchow/p/7533143.html
Copyright © 2020-2023  润新知