MySQL+EF Core的使用

本文详细阐述了如何在EF Core中利用MySQL进行开发,包括安装必要的NuGet包,如MySql.EntityFrameworkCore和Microsoft.EntityFrameworkCore.Tools,以及如何从现有数据库生成实体模型,无论是全部还是指定特定表。最后,文章提供了相关资源供进一步学习。
摘要由CSDN通过智能技术生成

本文介绍在EF Core中基于现有数据库的MySQL使用。

开发平台最低要求(Windows):

  • Connector/NET 8.0.22
  • MySQL Server 5.6
  • Entity Framework Core
  • .NET Standard 2.1 (.NET Core SDK 3.1 and Visual Studio 2019 version 16.5)

1.安装 NuGet 包

Install-Package MySql.EntityFrameworkCore -Version 5.0.5

Install-Package Microsoft.EntityFrameworkCore.Tools -Version 5.0.9

2.实体模型搭建

1.从现有数据库通过控制台命令生成实体框架模型:

Scaffold-DbContext “server=127.0.0.1;database=zhongtiesync;uid=root;password=123456;” MySql.EntityFrameworkCore -OutputDir sakila -f

2.指定表生成实体模型:

Scaffold-DbContext “server=127.0.0.1;database=zhongtiesync;uid=root;password=123456;” MySql.EntityFrameworkCore -OutputDir Sakila -Tables actor,film,language -f

using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;

#nullable disable

namespace Core.Entitys
{
   
    public partial class zhongtiesyncContext : DbContext
    {
   
        public zhongtiesyncContext()
        {
   
            // Creates the database if not exists
            this.Database.EnsureCreated();
        }

        public zhongtiesyncContext(DbContextOptions<zhongtiesyncContext> options)
            : base(options)
        {
   
        }

        public virtual DbSet<Department> Departments {
    get; set; }
        
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值