Create a new folder named Models in the project. Use below command in Package Manager Console to create entity classes from SQL Server database:
Scaffold-DbContext "Server=[Your Server Name];Database=[Your Database Name];user id=[username];password=[password];trusted_connection=true;encrypt=false" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Context DatabaseContext -f
Project structure after generating entity classes:

Account Entity
namespace EntityFrameworkCore_ConsoleApp.Models;
public partial class Account
{
public int Id { get; set; }
public string Username { get; set; } = null!;
public string Password { get; set; } = null!;
public string FullName { get; set; } = null!;
public bool Status { get; set; }
public virtual Contact? Contact { get; set; }
public virtual ICollection<Invoice> Invoices { get; } = new List<Invoice>();
public virtual ICollection<Role> Roles { get; } = new List<Role>();
}
Category Entity
namespace EntityFrameworkCore_ConsoleApp.Models;
public partial class Category
{
public int Id { get; set; }
public string Name { get; set; } = null!;
public virtual ICollection<Product> Products { get; } = new List<Product>();
}
Contact Entity
namespace EntityFrameworkCore_ConsoleApp.Models;
public partial class Contact
{
public int Id { get; set; }
public string Email { get; set; } = null!;
public string Address { get; set; } = null!;
public string Phone { get; set; } = null!;
public virtual Account IdNavigation { get; set; } = null!;
}
Invoice Entity
namespace EntityFrameworkCore_ConsoleApp.Models;
public partial class Invoice
{
public int Id { get; set; }
public string Name { get; set; } = null!;
public string Payment { get; set; } = null!;
public string Status { get; set; } = null!;
public DateTime Created { get; set; }
public int AccountId { get; set; }
public virtual Account Account { get; set; } = null!;
public virtual ICollection<InvoiceDetail> InvoiceDetails { get; } = new List<InvoiceDetail>();
}
InvoiceDetail Entity
namespace EntityFrameworkCore_ConsoleApp.Models;
public partial class InvoiceDetail
{
public int InvoiceId { get; set; }
public int ProductId { get; set; }
public int Quantity { get; set; }
public double Price { get; set; }
public virtual Invoice Invoice { get; set; } = null!;
public virtual Product Product { get; set; } = null!;
}
Product Entity
namespace EntityFrameworkCore_ConsoleApp.Models;
public partial class Product
{
public int Id { get; set; }
public string Name { get; set; } = null!;
public double Price { get; set; }
public int Quantity { get; set; }
public string? Description { get; set; }
public bool Status { get; set; }
public string? Photo { get; set; }
public DateTime Created { get; set; }
public int CategoryId { get; set; }
public virtual Category Category { get; set; } = null!;
public virtual ICollection<InvoiceDetail> InvoiceDetails { get; } = new List<InvoiceDetail>();
}
Role Entity
namespace EntityFrameworkCore_ConsoleApp.Models;
public partial class Role
{
public int Id { get; set; }
public string Name { get; set; } = null!;
public virtual ICollection<Account> Accounts { get; } = new List<Account>();
}
DatabaseContext Entity
using Microsoft.EntityFrameworkCore;
namespace EntityFrameworkCore_ConsoleApp.Models;
public partial class DatabaseContext : DbContext
{
public DatabaseContext()
{
}
public DatabaseContext(DbContextOptions<DatabaseContext> options) : base(options)
{
}
public virtual DbSet<Account> Accounts { get; set; }
public virtual DbSet<Category> Categories { get; set; }
public virtual DbSet<Contact> Contacts { get; set; }
public virtual DbSet<Invoice> Invoices { get; set; }
public virtual DbSet<InvoiceDetail> InvoiceDetails { get; set; }
public virtual DbSet<Product> Products { get; set; }
public virtual DbSet<Role> Roles { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Server=[Your Server Name];Database=[Your Database Name];user id=[username];password=[password];trusted_connection=true;encrypt=false");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Account>(entity =>
{
entity.HasKey(e => e.Id).HasName("PK__Account__3214EC078D0DCDDD");
entity.ToTable("Account");
entity.Property(e => e.FullName)
.HasMaxLength(250)
.IsUnicode(false);
entity.Property(e => e.Password)
.HasMaxLength(250)
.IsUnicode(false);
entity.Property(e => e.Username)
.HasMaxLength(250)
.IsUnicode(false);
entity.HasMany(d => d.Roles).WithMany(p => p.Accounts)
.UsingEntity<Dictionary<string, object>>(
"AccountRole",
r => r.HasOne<Role>().WithMany()
.HasForeignKey("RoleId")
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK__AccountRo__RoleI__2C3393D0"),
l => l.HasOne<Account>().WithMany()
.HasForeignKey("AccountId")
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK__AccountRo__Accou__2B3F6F97"),
j =>
{
j.HasKey("AccountId", "RoleId").HasName("PK__AccountR__8C32094788DC9780");
j.ToTable("AccountRole");
});
});
modelBuilder.Entity<Category>(entity =>
{
entity.HasKey(e => e.Id).HasName("PK__Category__3214EC07409A2C3D");
entity.ToTable("Category");
entity.Property(e => e.Name)
.HasMaxLength(250)
.IsUnicode(false);
});
modelBuilder.Entity<Contact>(entity =>
{
entity.HasKey(e => e.Id).HasName("PK__Contact__3214EC07792F3EFA");
entity.ToTable("Contact");
entity.Property(e => e.Id).ValueGeneratedNever();
entity.Property(e => e.Address)
.HasMaxLength(250)
.IsUnicode(false);
entity.Property(e => e.Email)
.HasMaxLength(250)
.IsUnicode(false);
entity.Property(e => e.Phone)
.HasMaxLength(50)
.IsUnicode(false);
entity.HasOne(d => d.IdNavigation).WithOne(p => p.Contact)
.HasForeignKey<Contact>(d => d.Id)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK__Contact__Id__286302EC");
});
modelBuilder.Entity<Invoice>(entity =>
{
entity.HasKey(e => e.Id).HasName("PK__Invoice__3214EC072A8BFC4E");
entity.ToTable("Invoice");
entity.Property(e => e.Created).HasColumnType("date");
entity.Property(e => e.Name)
.HasMaxLength(250)
.IsUnicode(false);
entity.Property(e => e.Payment)
.HasMaxLength(250)
.IsUnicode(false);
entity.Property(e => e.Status)
.HasMaxLength(250)
.IsUnicode(false);
entity.HasOne(d => d.Account).WithMany(p => p.Invoices)
.HasForeignKey(d => d.AccountId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK__Invoice__Account__30F848ED");
});
modelBuilder.Entity<InvoiceDetail>(entity =>
{
entity.HasKey(e => new { e.InvoiceId, e.ProductId }).HasName("PK__InvoiceD__1CD666D93505D892");
entity.HasOne(d => d.Invoice).WithMany(p => p.InvoiceDetails)
.HasForeignKey(d => d.InvoiceId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK__InvoiceDe__Invoi__36B12243");
entity.HasOne(d => d.Product).WithMany(p => p.InvoiceDetails)
.HasForeignKey(d => d.ProductId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK__InvoiceDe__Produ__37A5467C");
});
modelBuilder.Entity<Product>(entity =>
{
entity.HasKey(e => e.Id).HasName("PK__Product__3214EC071BF08D02");
entity.ToTable("Product");
entity.Property(e => e.Created).HasColumnType("date");
entity.Property(e => e.Description).HasColumnType("text");
entity.Property(e => e.Name)
.HasMaxLength(250)
.IsUnicode(false);
entity.Property(e => e.Photo)
.HasMaxLength(250)
.IsUnicode(false);
entity.HasOne(d => d.Category).WithMany(p => p.Products)
.HasForeignKey(d => d.CategoryId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK__Product__Categor__33D4B598");
});
modelBuilder.Entity<Role>(entity =>
{
entity.HasKey(e => e.Id).HasName("PK__Role__3214EC07D5159758");
entity.ToTable("Role");
entity.Property(e => e.Name)
.HasMaxLength(250)
.IsUnicode(false);
});
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}