EF Core AUTO_INCREMENT problem with MySQL database

Stefan 0 Reputation points
2024-07-11T22:03:48.4833333+00:00

The only problem i have now that i finished my database design is that the AUTO_INCREMENT is not specified when creating the tables by EF Core.

This is the modelBuilder

modelBuilder.Entity<Conturi>(entity =>
{
    entity.ToTable("conturi");
    entity.HasKey(e => e.IdCont);
    entity.Property(e => e.IdCont)
        .HasColumnName("id_cont")
        .HasColumnType("integer")
        .ValueGeneratedOnAdd();
    entity.Property(e => e.Nume)
        .HasMaxLength(10)
        .HasColumnName("nume")
        .HasColumnType("varchar");
    
    entity.Property(e => e.Prenume)
        .HasMaxLength(20)
        .HasColumnName("prenume")
        .HasColumnType("varchar");
    
    entity.Property(e => e.Gen)
        .HasColumnName("gen")
        .HasColumnType("tinyint");
    entity.Property(e => e.NrTelefon)
        .HasColumnName("nr_telefon")
        .HasColumnType("varchar")
        .HasMaxLength(10);
    entity.Property(e => e.Username)
        .HasColumnName("username")
        .HasColumnType("varchar")
        .HasMaxLength(15)
        .IsRequired();
    
    entity.Property(e => e.Email)
        .HasColumnName("email")
        .HasColumnType("varchar")
        .HasMaxLength(50)
        .IsRequired();
    
    entity.Property(e => e.Parola)
        .HasColumnName("parola")
        .HasColumnType("varchar")
        .HasMaxLength(150)
        .IsRequired();
    entity.Property(e => e.DataCreare)
        .HasColumnName("data_creare")
        .HasColumnType("datetime")
        .HasDefaultValueSql("NOW()");
    
    entity.Property(e => e.CodActivare)
        .HasColumnName("cod_activare")
        .HasColumnType("varchar")
        .HasMaxLength(100)
        .IsRequired();
    
    entity.Property(e => e.Verificat)
        .HasColumnName("verificat")
        .HasColumnType("tinyint")
        .IsRequired();
    
    entity.Property(e => e.Rol)
        .HasColumnName("rol")
        .HasColumnType("varchar")
        .HasMaxLength(15)
        .IsRequired();
    
    entity.HasMany(e => e.AdreseConturi)
        .WithOne(e => e.Cont)
        .HasForeignKey(e => e.IdCont)
        .IsRequired();
});

This is the entity model :

public  class Conturi : ICloneable
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int IdCont { get; init; } //
    
    [StringLength(10)]
    public string? Nume { get; init; } // 
    
    [StringLength(20)]
    public string? Prenume { get; init; } // 
    
    public bool? Gen { get; init; } //
    
    [StringLength(10)]
    public string? NrTelefon { get; init; } // 
    
    [StringLength(15)]
    public string Username { get; init; } // 
    
    [StringLength(50)]
    public string Email { get; init; }
    
    [StringLength(150)]
    public string Parola { get; init; }
    
    public  DateTime? DataCreare { get; init; }
    
    [StringLength(100)]
    public string CodActivare { get; set; }
    
    public bool Verificat { get; set; }
    
    [StringLength(15)]
    public string Rol { get; init; }
    public  ICollection<Adrese>? AdreseConturi { get;}
    public Conturi()
    {
        
    }
    public Conturi(string? nume, string? prenume, bool? gen, string? nrTelefon, string username, 
                   string email, string parola, DateTime? dataCreare, 
                   string codActivare, bool verificat, string rol , ICollection<Adrese>? adreseConturi)
    {
        Nume = nume;
        Prenume = prenume;
        Gen = gen;
        NrTelefon = nrTelefon;
        Username = username;
        Email = email;
        Parola = parola;
        DataCreare = dataCreare;
        CodActivare = codActivare;
        Verificat = verificat;
        Rol = rol;
        AdreseConturi = adreseConturi == null ? new HashSet<Adrese>() : new HashSet<Adrese>(adreseConturi);
       
    }
    // Copy Constructor
    public Conturi(Conturi other) 
    {
        ArgumentNullException.ThrowIfNull(other);
        
        IdCont = other.IdCont;
        Nume = other.Nume;
        Prenume = other.Prenume;
        Gen = other.Gen;
        NrTelefon = other.NrTelefon;
        Username = other.Username;
        Email = other.Email;
        Parola = other.Parola;
        DataCreare = other.DataCreare;
        CodActivare = other.CodActivare;
        Verificat = other.Verificat;
        Rol = other.Rol;
        AdreseConturi = other.AdreseConturi != null ? new HashSet<Adrese>(other.AdreseConturi) : new HashSet<Adrese>();
    }
    
    public object Clone()
    {
        return new Conturi(this);
    }
}

I got the latest version of Pomelo , EF Core , etc.

and this is how i see it in my DataGrip after running the migrations and updating the database.

micro.png

Why it is no AUTO_INCREMENT selected there , even tho i specified in my EF Core fluent API , even in my model ?

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
743 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Hongrui Yu-MSFT 2,075 Reputation points Microsoft Vendor
    2024-07-15T01:59:36.52+00:00

    Hi,@Stefan.

    After testing, the following method is feasible. You could refer to it to design your program.

     

    Code First Run ResultsPicture1

    Related code:

    
    internal class MyDbContext : DbContext
    
    {
    
        public MyDbContext()
    
     
    
        {
    
     
    
        }
    
     
    
        public MyDbContext(DbContextOptions<MyDbContext> options)
    
     
    
            : base(options)
    
        {
    
     
    
        }
    
     
    
        public virtual DbSet<Student> Students { get; set; }
    
     
    
     
    
     
    
     
    
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    
     
    
            => optionsBuilder.UseMySql("Write your database connection string", new MySqlServerVersion("8.0.38"));
    
     
    
        protected override void OnModelCreating(ModelBuilder modelBuilder)
    
        {
    
            modelBuilder
    
                .UseCollation("utf8_unicode_ci")
    
                .HasCharSet("utf8");
    
     
    
            modelBuilder.Entity<Student>(entity =>
    
            {
    
                entity.HasKey(e => e.Id).HasName("PRIMARY");
    
     
    
                entity.ToTable("student");
    
     
    
                entity.Property(e => e.Id).HasColumnType("int(11)");
    
                entity.Property(e => e.Name).HasMaxLength(255);
    
            });
    
        }
    
    }
    
     
    
    internal class Student
    
    {
    
            public int Id { get; set; }
    
     
    
            public string Name { get; set; }
    
    }
    
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.