当前位置: 首页 > 软件库 > 数据库相关 > >

mysql-dotnet-core

授权协议 Readme
开发语言 C/C++
所属分类 数据库相关
软件类型 开源软件
地区 不详
投 递 者 贺功
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

MySQL ASP.NET 5.0

Convert an ASP.NET Core Web Application project to use MySQL with Entity Framework, enabling development on macOS, linux, or Windows targets using IDEs such as VS Code, Visual Studio, or JetBrains Rider.

This project uses .NET 5.0 target framework, ASP.NET Core Web Application (Model-View-Controller) project scaffold from Visual Studio 2019 (version 16.10.1) to connect to MySQL 8.0.

vscode

For previous versions of .NET Core 3.x, 2.x, 1.x, see the releases for past implementations in this repository.

Quick Start

To immediately use this solution, make sure your environment setup is complete; then, jump to running the solution.

Environment Setup

Make sure you have the .NET 5.0 SDK installed on your system.

If you're using Visual Studio Code, you will need to generate ASP.NET Core developer certificates by issuing the following commands from a terminal:

dotnet dev-certs https --clean
dotnet dev-certs https

For command line database ef commands, you will need to install Entity Framework Core tools .NET CLI:

dotnet tool install --global dotnet-ef

Make sure you have MySQL 8.0 Server installed on your system; or, use a Docker image instead of installing MySQL Server. In a terminal, execute the following to spin up a Docker image of MySQL:

docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=mypassword -d mysql

Running the solution

Before the solution can be executed, Entity Framework migrations must be run to setup the database.

Configure connection string in project's appsettings.json, replacing the username, password, and database appropriately:

"ConnectionStrings": {
  "DefaultConnection":"server=localhost;userid=myusername;password=mypassword;database=mydatabase;"
},

Execute the migration using either Visual Studio Package Manager Console (from menu: Tools -> NuGet Package Manager -> Package Manager Console):

>> Update-Database

Or, from the command line via DotNet CLI, execute the following command inside the project directory, where the .csproj file is located:

$ dotnet ef database update

After running the migration, the database is created and web application is ready to be run.

Run the solution via your IDE; or, execute the following command line

dotnet run

Then, load via browser to either https or http endpoints:

Project Setup

Project setup has already been completed in this repository, ready for use as a template for your next project.

Otherwise, adapt the steps below to incorporate MySQL into your solution.

Install NuGet packages

Install the MySql.EntityFrameworkCore NuGet package in the ASP.NET web application.

To do this, you can use the dotnet command line by executing:

dotnet add package MySql.EntityFrameworkCore --version 5.0.3.1

Or, edit the project's .csproj file and add the following line in the PackageReference item group:

<PackageReference Include="MySql.EntityFrameworkCore" Version="5.0.3.1" />

Modify Startup.cs

In Startup.cs under ConfigureServices() method, replace the UseSqlServer / UseSqlite option with MySQL:

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddDbContext<ApplicationDbContext>(options =>
        options.UseMySQL(Configuration.GetConnectionString("DefaultConnection")));

Migration Issues with DbContext

Upon upgrading MySQL Oracle Connector, Entity Framework migrations may fail with the errors:

MySql.Data.MySqlClient.MySqlException (0x80004005): Specified key was too long; max key length is 3072 bytes

MySql.Data.MySqlClient.MySqlException (0x80004005): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'max) NULL, PRIMARY KEY (Id))

Failed executing DbCommand (2ms) [Parameters=[], CommandType='Text', CommandTimeout='30']

CREATE TABLE `AspNetRoles` (
    `Id` TEXT NOT NULL,
    `Name` TEXT NULL,
    `NormalizedName` TEXT NULL,
    `ConcurrencyStamp` TEXT NULL,
    PRIMARY KEY (`Id`)
);

MySql.Data.MySqlClient.MySqlException (0x80004005): BLOB/TEXT column 'Id' used in key specification without a key length

CREATE TABLE `AspNetRoles` (
    `Id` nvarchar(450) NOT NULL,
    `Name` nvarchar(256) NULL,
    `NormalizedName` nvarchar(256) NULL,
    `ConcurrencyStamp` nvarchar(max) NULL,
    PRIMARY KEY (`Id`)
);

MySql.Data.MySqlClient.MySqlException (0x80004005): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'max) NULL,

To resolve this, add the following code within the ApplicationDbContext.cs OnModelCreating().

using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

public class ApplicationDbContext : IdentityDbContext
{

    // ...

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        builder.Entity<IdentityRole>(entity => entity.Property(m => m.Id).HasMaxLength(450));
        builder.Entity<IdentityRole>(entity => entity.Property(m => m.ConcurrencyStamp).HasColumnType("varchar(256)"));

        builder.Entity<IdentityUserLogin<string>>(entity =>
        {
            entity.Property(m => m.LoginProvider).HasMaxLength(127);
            entity.Property(m => m.ProviderKey).HasMaxLength(127);
        });

        builder.Entity<IdentityUserRole<string>>(entity =>
        {
            entity.Property(m => m.UserId).HasMaxLength(127);
            entity.Property(m => m.RoleId).HasMaxLength(127);
        });

        builder.Entity<IdentityUserToken<string>>(entity =>
        {
            entity.Property(m => m.UserId).HasMaxLength(127);
            entity.Property(m => m.LoginProvider).HasMaxLength(127);
            entity.Property(m => m.Name).HasMaxLength(127);
        });
    }

Then, generate a new migration using Visual Studio Package Manager Console (from menu: Tools -> NuGet Package Manager -> Package Manager Console):

>> Add-Migration

Or, from the command line via DotNet CLI:

$ dotnet ef migrations add CreateIdentitySchema

Troubleshooting

Create Entity Framework Migration Table in MySQL

If running dotnet ef fails initially, the __efmigrationshistory table may not exist. Past versions of Entity Framework migration tools failed to create this table.

Assure you're running the lastest tools:

dotnet tool update --global dotnet-ef

Otherwise, manually create the migrations history table in the MySQL database by executing the following SQL script.

use mydatabase;

CREATE TABLE `mydatabase`.`__EFMigrationsHistory` (
  `MigrationId` text NOT NULL,
  `ProductVersion` text NOT NULL,
  PRIMARY KEY (`MigrationId`(255)));

Deprecated MySQL NuGet Packages

Note that MySql.Data.EntityFrameworkCore NuGet package is deprecated, and is now: MySql.EntityFrameworkCore.

  • 先来回答以下问题。 1.什么是cms? Content Management System,内容管理系统。 2.dotnetcore是什么? .NET Core,是由Microsoft开发,目前在.NET Foundation(一个非营利的开源组织)下进行管理,采用宽松的MIT协议,可构建各种软件,包括Web应用程序、移动应用程序、桌面应用程序、云服务、微服务、API、游戏和物联网应用程序。 3.

  • 一安装Mysql  转载自 http://www.centoscn.com/mysql/2016/0315/6844.html      CentOS 7 安装 MySQL   环境 CentOS 7.1 (64-bit system) MySQL 5.6.24 CentOS 安装 参考:http://www.waylau.com/centos-7-installation-and-configu

  • 我们正在尝试通过.NET Core连接到MySQL数据库。 一切都在本地运行,但在运行gitlab-ci的服务器上,构建失败。 因此,构建失败。 引发的错误: error NU1001: The dependency MySql.Data >= 7.0.6-IR31 could not be resolved. 这很奇怪,因为它适用于我们的本地计算机,而不适用于在docker中运行的CI。 运行d

  • 在.net frameworks的ef里连接mysql我们已经测试通过了,而在dotnet core里的efCore上去连接mysql我们需要测试一下,并且在测试过程中出现了一些问题,当然最后也是解决了,下面总结一下,分享给大家! mysql项目的依赖包 数据上下文和连接串 数据仓储 添加模块扩展 业务层注入 业务实现 mysql项目的依赖包 Microsoft.EntityFrameworkCo

  • 这学期开学就要做一个大型的课设,本着胡乱折腾认真学习的宗旨,我把电脑折腾成了ubuntu,觊觎dotnetcore许久,终于可以体验一下跨平台开发 了. 开发环境:dotnet2.0+MySql+ubuntu16.04×64 首先安装vscode,不必多言,安装完成后在vscode中安装nuget插件 然后终端安装dotnet,这个着实费了一丢丢功夫,最佳的解决方案还是参考微软官方的文档:curl

  • 用Scaffold命令生成数据库实体类,新建个asp.net core 2.2 webapi项目,在.net core 2.2下是好的,能生成实体类。再新建asp.net core 3.0 webapi项目,关联的驱动和组件都升级到3.0 预览版,同样的Scaffold命令就报错了,不知道问题再哪里?目前就现在2.2里生成实体类,在复制到3.0的项目中用。 PM> Scaffold-DbConte

  • using MySql.Data.MySqlClient; using Newtonsoft.Json; using System; using System.Collections.Generic; using Dapper; namespace ConsoleApp7 {     class Program     {         static void Main(string[] arg

 相关资料
  • dotNet Protector是一个功能强大的.NET代码保护系统,它可以防止程序集被反编译。dotNet Protector使用的是一个新的主体混淆技术保护应用程序和组件。程序集不再需要合并到一个win32可执行文件中,但是会保留其.net特性。 dotNet Protector 采用一个命名混淆器结合一个主体混淆器来保护您的代码。 dotNet Protector 本地运行支持X32、X64

  • Playwright for .NET �� Linux macOS Windows Chromium 94.0.4595.0 ✅ ✅ ✅ WebKit 15.0 ✅ ✅ ✅ Firefox 91.0 ✅ ✅ ✅ Playwright for .NET is the official language port of Playwright, the library to automate Chro

  • GraphQL for .NET This is an implementation of Facebook's GraphQL in .NET. Now the specification is being developed by theGraphQL Foundation. This project uses a lexer/parser originally writtenby Marek

  • What's new with Bot Framework This repository contains code for the .NET version of the Microsoft Bot Framework SDK, which is part of the Microsoft Bot Framework - a comprehensive framework for buildi

  • Moved to a centralized example repository: https://github.com/getsentry/examples/tree/master/dotnet

  • sonar-dotnet-codeanalysis 是 Sonar 平台用于分析 C# 语言代码质量的插件。 用于C#的SonarQube代码分析是一个代码分析NuGet包和一个基于.NET编译器平台(“ Roslyn”)的Visual Studio扩展。该软件包提供有关错误,潜在错误,重复代码,编码标准,注释和软件设计的报告。 安装 在“ Visual Studio扩展和更新”窗口中。选择“在线