An Enthusiastic Programmer

From Existing Database

|

Here, I will illustrate how to generate Context and Entities from an existing database. As the Entity Framework Core doesn’t provide a visual designer, so we need to use the Scaffold-DbContext command.

I have a database which already populated.

Alt

Scaffold-DbContext Command

Use Scaffold-DbContext to create models is easy. get its syntax and details by running the get-help entityframeworkcore or get-help Scaffold-DbContext command on Package Manager Console:

Scaffold-DbContext [-Connection] <String> [-Provider] <String> [-OutputDir <String>] [-ContextDir <String>] [-Context <String>] [-Schemas <String[]>] [-Tables <String[]>] [-DataAnnotations] [-UseDatabaseNames] [-Force] [-Project <String>] [-StartupProject <String>] [<CommonParameters>]

In this situation, run the following code on NuGet package manager:

PM> Scaffold-DbContext "Server=.;Database=SchoolDB;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

it will create a models folder, and put the entity and context class into it.

Alt

The generated code:

public partial class SchoolDBContext : DbContext
{
    public SchoolDBContext(){}
    public SchoolDBContext(DbContextOptions<SchoolDBContext> options): base(options){}
    public virtual DbSet<Classroom> Classroom { get; set; }
    public virtual DbSet<Monitor> Monitor { 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.UseSqlServer("Server=.;Database=SchoolDB;Trusted_Connection=True;");
        }
    }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Classroom>(entity =>
        {
            entity.Property(e => e.Id).HasColumnName("id");
            entity.Property(e => e.Name).HasColumnName("name");
        });
        modelBuilder.Entity<Monitor>(entity =>
        {
            entity.HasIndex(e => e.Classroomid);
            entity.Property(e => e.Id).HasColumnName("id");
            entity.Property(e => e.Classroomid).HasColumnName("classroomid");
            entity.Property(e => e.Name).HasColumnName("name");
            entity.HasOne(d => d.Classroom)
                .WithMany(p => p.Monitor)
                .HasForeignKey(d => d.Classroomid);
        });
        OnModelCreatingPartial(modelBuilder);
    }
    partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
public partial class Monitor
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Classroomid { get; set; }
    public virtual Classroom Classroom { get; set; }
}
public partial class Classroom
{
    public Classroom(){Monitor = new HashSet<Monitor>();}
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Monitor> Monitor { get; set; }
}

As you can see, what’s applied is that the Fluent-API. However, if you want to use the DataAnnotations, then you should add the -DataAnnotations at the end of the command.

PM> Scaffold-DbContext "Server=.;Database=SchoolDB;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -DataAnnotations

the generated code:

public partial class SchoolDBContext : DbContext
{
    public SchoolDBContext(){}
    public SchoolDBContext(DbContextOptions<SchoolDBContext> options): base(options){}
    public virtual DbSet<Classroom> Classroom { get; set; }
    public virtual DbSet<Monitor> Monitor { 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.UseSqlServer("Server=.;Database=SchoolDB;Trusted_Connection=True;");
        }
    }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Monitor>(entity =>
        {
            entity.HasIndex(e => e.Classroomid);
        });
        OnModelCreatingPartial(modelBuilder);
    }
    partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
public partial class Monitor
{
    [Key]
    [Column("id")]
    public int Id { get; set; }
    [Column("name")]
    public string Name { get; set; }
    [Column("classroomid")]
    public int Classroomid { get; set; }
    [ForeignKey(nameof(Classroomid))]
    [InverseProperty("Monitor")]
    public virtual Classroom Classroom { get; set; }
}
public partial class Classroom
{
    public Classroom(){Monitor = new HashSet<Monitor>();}
    [Key]
    [Column("id")]
    public int Id { get; set; }
    [Column("name")]
    public string Name { get; set; }
    [InverseProperty("Classroom")]
    public virtual ICollection<Monitor> Monitor { get; set; }
}

Here it is the DataAnnotation now!

Comments