Nuget Packages
Install-Package Microsoft.EntityFrameworkCore
Install-Package Microsoft.EntityFrameworkCore.Relational
Install-Package Microsoft.EntityFrameworkCore.SqlServer
Install-Package Microsoft.EntityFrameworkCore.Tools
//------------------------------------------------------------------------
EF-Core adding example
Commands used to Create/Update Database (code first method)
Usage Example
If stored procedure returns a complex result, define a DTO (Data Transfer Object):
public class StudentDTO
{
public int Id { get; set; }
public string Name { get; set; }
}
public class context : DbContext
{
public context(DbContextOptions<context> options)
: base(options)
{
}
// Map DTO to DbSet (not tracked as a table, but needed for FromSqlRaw)
public DbSet<StudentDTO> StudentDTOs { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Configure DTO as a keyless entity since it doesn’t map to a table
modelBuilder.Entity<StudentDTO>().HasNoKey().ToView(null); // Explicitly mark as not mapped to a database view
;
}
}
var students = context.StudentDTOs.FromSqlRaw("EXEC GetStudentDetails").ToList();
//Note****************************
• EF Core migrations only generate tables for entities with keys (i.e., those mapped to actual tables).
• By marking StudentDTO as HasNoKey, you’re telling EF Core that this type is keyless and should be treated as a query type (used only for mapping raw SQL or views).
• Query types are not included in migrations, so no table will be created for StudentDTO.
So your StudentDTO is safe — it’s purely a DTO for stored procedure results, not a database entity.
👉 If you want EF Core to create a table, you’d need to define a key (e.g., HasKey(x=>x.id) ), but for stored procedure DTOs, you should keep it keyless.
//======================================================================================
using Microsoft.EntityFrameworkCore;
public class AppDbContext : DbContext
{
public DbSet<Product> Products { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Server=localhost;Database=MyDatabase;Trusted_Connection=True;");
}
}
//----------------------------------------------WPF-------------------------------------------------------------------------------
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var connectionString = AppConfig.Configuration.GetConnectionString("MyDb");
optionsBuilder.UseSqlServer(connectionString);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// If MyKeylessEntity exists, ensure it's imported and defined.
// Otherwise, remove or replace this block.
// Example:
modelBuilder.Entity<Preference>(entity =>
{
entity.HasNoKey();
});
}
//-------------------------------------------------------------
public static class AppConfig
{
public static IConfiguration Configuration { get; }
static AppConfig()
{
Configuration = new ConfigurationBuilder()
.SetBasePath(AppContext.BaseDirectory) //This extension method is in Microsoft.Extensions.Configuration.FileExtensions
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) // This extension method is in Microsoft.Extensions.Configuration.Json
.Build();
}
public static string GetConnectionString(string name)
{
return Configuration.GetConnectionString(name);
}
}