PomeloFoundation / Pomelo.EntityFrameworkCore.MySql

Entity Framework Core provider for MySQL and MariaDB built on top of MySqlConnector
MIT License
2.68k stars 382 forks source link

EF Core 6, [Required] DataAnnotation is not created by the database scaffold #1656

Open florentmsl opened 2 years ago

florentmsl commented 2 years ago

Steps to reproduce

Create the table:

CREATE TABLE application_user(

  id bigint NOT NULL AUTO_INCREMENT COMMENT 'Primary key',

  username varchar(50) NOT NULL COMMENT 'username',
  ...
  PRIMARY KEY (id),
  UNIQUE KEY application_user_uk01 (username),
  UNIQUE KEY application_user_uk02 (email_address)
) COMMENT='Table for managing all users of the system';

and then scaffold the DB

dotnet ef dbcontext scaffold "$conn_str" $db_provider --project $project_path --context DataContext --context-dir Context --context-namespace Database.Context --namespace Database.Entities --output-dir Entities --schema $db_schema --data-annotations --no-onconfiguring --no-pluralize --no-build --verbose --force

the variables are:

$db_schema = "db"
$conn_str = "Server=127.0.0.1;Database=$db_schema;Uid=...;Pwd=...;"
$db_provider = "Pomelo.EntityFrameworkCore.MySql"
$project_path = "..\..\Database\"

The issue

and it works just fine, only the [Required] data annotation for e.g the username is not created.

This is the model which is generated:

    [Table("application_user")]
    [Index("Username", Name = "application_user_uk01", IsUnique = true)]
    [Index("EmailAddress", Name = "application_user_uk02", IsUnique = true)]
    public partial class ApplicationUser
    {
        /// <summary>
        /// Primary key
        /// </summary>
        [Key]
        [Column("id")]
        public long Id { get; set; }
        /// <summary>
        /// username
        /// </summary>
        [Column("username")]
        [StringLength(50)]
        public string Username { get; set; }

Further technical details

MySQL version: 8.0.29 (Docker) Operating system: Windows 11 Pomelo.EntityFrameworkCore.MySql version: 6.0.1 .NET version: 6

lauxjpn commented 2 years ago

@Florent22 I am unable to reproduce this issue.#

I setup a database using the following SQL:

DROP DATABASE IF EXISTS `Issue1656`;
CREATE DATABASE `Issue1656` CHARACTER SET utf8mb4;
USE `Issue1656`;

CREATE TABLE application_user(
  id bigint NOT NULL AUTO_INCREMENT COMMENT 'Primary key',
  username varchar(50) NOT NULL COMMENT 'username',

  PRIMARY KEY (id),
  UNIQUE KEY application_user_uk01 (username)
) COMMENT='Table for managing all users of the system';

Then I run the following command:

dotnet ef dbcontext scaffold 'server=127.0.0.1;port=3308;user=root;password=;database=Issue1656' 'Pomelo.EntityFrameworkCore.MySql' --data-annotations --no-onconfiguring --no-pluralize --no-build --verbose --force

The file of the generated ApplicationUser entity looks like this:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;

namespace IssueConsoleTemplate
{
    /// <summary>
    /// Table for managing all users of the system
    /// </summary>
    [Table("application_user")]
    [Index(nameof(Username), Name = "application_user_uk01", IsUnique = true)]
    public partial class ApplicationUser
    {
        /// <summary>
        /// Primary key
        /// </summary>
        [Key]
        [Column("id")]
        public long Id { get; set; }
        /// <summary>
        /// username
        /// </summary>
        [Required]
        [Column("username")]
        [StringLength(50)]
        public string Username { get; set; }
    }
}

It contains the expected [Required] attribute.

Tested with Pomelo 6.0.1 and EF Core 6.0.1.


@Florent22 To further investigate the issue (that is likely on your end), run the following command using the MySQL command line client and verify, that the output is indeed NOT NULL for the username column:

mysql -h YOUR_HOST -u YOUR_USER -pYOUR_PASSWORD YOUR_DATABASE -e "DESCRIBE application_user; SHOW CREATE TABLE application_user;"


(Unrelated to your reported issue, using --schema $db_schema has no effect, because does not use the concept of EF Core schemas, which stem from SQL Server schemas and are a way to categorize multiple tables. The EF Core concept of a schema does not translate to the MySQL concept of a schema/database.)