.NetCore中EFCore for MySql整理(三)之Pomelo.EntityFrameworkCore.MySql

1、Pomelo.EntityFrameworkCore.MySql簡介html

Git源代碼地址:https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySqlgit

注:這是第三方的 EF Core 的ORM框架,支持Synac I/O訪問操做MySql數據,不是MySql官方的數據驅動。github

1.本框架支持Code First 和Server First 數據庫

問題:Server First 生成代碼沒有附帶主外鍵關聯。框架

2、Server First方式使用示例ide

1.建立.Net Core 控制檯項目工具

2.安裝Pomelo.EntityFrameworkCore.MySql測試

使用命令:ui

Install-Package Pomelo.EntityFrameworkCore.MySql

或者在包管理工具中搜索安裝spa

建立數據庫角色-菜單命令:

-- 建立角色&菜單簡單邏輯表
create database RoleMenu;
grant all on *.* to 'userone'@'localhost' identified by '123';

use RoleMenu;
-- drop table Role_Menu,Role,Menu;
-- 建立角色表
create table Role(
RoleID int not null  auto_increment,
Name nvarchar(50) not null,
SortValue int not null,
primary key(RoleID)
);

-- 建立菜單表 
create table Menu(
MenuID int not null auto_increment,
Name nvarchar(50) not null,
Title nvarchar(100) not null,
LinkUrl varchar(200) null,
Icon varchar(100) null,
primary key(MenuID)
);

-- 建立角色-菜單表 
create table Role_Menu(
ID int not null auto_increment,
RoleID int not null,
MenuID int not null,
primary key(ID),
foreign key(RoleID) references Role(RoleID)
on delete cascade,
foreign key(MenuID) references Menu(MenuID)
on delete no action
);

-- 添加測試數據 
insert Role(Name,SortValue) values('系統管理員',1);
insert Role(Name,SortValue) values('服務中心',2);

-- 添加菜單數據 
insert Menu(Name,Title) values('我的信息','我的信息管理');
insert Menu(Name,Title) values('修改密碼','修改登陸密碼&二級密碼');

-- 添加關聯 
insert Role_Menu(RoleID,MenuID) values(1,1);
insert Role_Menu(RoleID,MenuID) values(1,2);
insert Role_Menu(RoleID,MenuID) values(2,1);
insert Role_Menu(RoleID,MenuID) values(2,2);
View Code

使用PM命令,連接數據庫生成model層和上下文:

Scaffold-DbContext "Server=127.0.0.1;port=3306;Database=Md5Data;uid=xxx;pwd=xxx;Character Set=utf8;" MySql.Data.EntityFrameworkCore -OutputDir models2 

生成代碼結果:

    public partial class Role
    {
        public int RoleId { get; set; }
        public string Name { get; set; }
        public int SortValue { get; set; }
    }
    public partial class Menu
    {
        public int MenuId { get; set; }
        public string Name { get; set; }
        public string Title { get; set; }
        public string LinkUrl { get; set; }
        public string Icon { get; set; }
    }
    public partial class RoleMenu
    {
        public int Id { get; set; }
        public int RoleId { get; set; }
        public int MenuId { get; set; }
    }
 public partial class RoleMenuContext : DbContext
    {
        public virtual DbSet<Menu> Menu { get; set; }
        public virtual DbSet<Role> Role { get; set; }
        public virtual DbSet<RoleMenu> RoleMenu { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.
                optionsBuilder.UseMySql("Server=127.0.0.1;port=3306;Database=RoleMenu;uid=userone;pwd=123;Character Set=utf8;");
            }
        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Menu>(entity =>
            {
                entity.Property(e => e.MenuId)
                    .HasColumnName("MenuID")
                    .HasColumnType("int(11)");

                entity.Property(e => e.Icon).HasMaxLength(100);

                entity.Property(e => e.LinkUrl).HasMaxLength(200);

                entity.Property(e => e.Name)
                    .IsRequired()
                    .HasMaxLength(50);

                entity.Property(e => e.Title)
                    .IsRequired()
                    .HasMaxLength(100);
            });

            modelBuilder.Entity<Role>(entity =>
            {
                entity.Property(e => e.RoleId)
                    .HasColumnName("RoleID")
                    .HasColumnType("int(11)");

                entity.Property(e => e.Name)
                    .IsRequired()
                    .HasMaxLength(50);

                entity.Property(e => e.SortValue).HasColumnType("int(11)");
            });

            modelBuilder.Entity<RoleMenu>(entity =>
            {
                entity.ToTable("Role_Menu");

                entity.HasIndex(e => e.MenuId)
                    .HasName("MenuID");

                entity.HasIndex(e => e.RoleId)
                    .HasName("RoleID");

                entity.Property(e => e.Id)
                    .HasColumnName("ID")
                    .HasColumnType("int(11)");

                entity.Property(e => e.MenuId)
                    .HasColumnName("MenuID")
                    .HasColumnType("int(11)");

                entity.Property(e => e.RoleId)
                    .HasColumnName("RoleID")
                    .HasColumnType("int(11)");
            });
        }
    }

 

更多:

.NetCore中EFCore for MySql整理(二)

.NetCore中EFCore for MySql整理

.NetCore中EFCore的使用整理

相關文章
相關標籤/搜索