Navigation Property
[ForeignKey("Villa")]
public int VillaId { get; set; }
public Villa Villa { get; set; }
------------------------------------------
public int VillaId { get; set; }
[ForeignKey("VillaId")]
public Villa Villa { get; set; }
-------------------------------------------
// wpf
Scaffold-Dbcontext "Server=.;Database=MyCompany;Trusted_Connection=True;TrustedServerCertificate=True" Microsoft.EntityFrameworkCore.SqlServer -OutputDir EF\Models -Force
//=========================================================================
EntityFrameworkCore
//---------------------------------------------------------------------------------------------------------------------------
Install-Package Microsoft.EntityFrameworkCore
Install-Package Microsoft.EntityFrameworkCore.SqlServer
Install-Package Microsoft.EntityFrameworkCore.Tools
//------------------------------------------------------------------------
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
//--------------------------------------------------------------------------
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;");
}
}
//----------------------------------------------------------------------------
using System;
using System.Linq;
class Program
{
static void Main()
{
using (var context = new AppDbContext())
{
// Add a new product
context.Products.Add(new Product { Name = "Laptop", Price = 1200 });
context.SaveChanges();
// Retrieve and display products
var products = context.Products.ToList();
foreach (var product in products)
{
Console.WriteLine($"ID: {product.Id}, Name: {product.Name}, Price: {product.Price}");
}
}
}
}
//-----------------------------------------------------------------------
Add-Migration InitialCreate
Update-Database
//==========================================================================
EFCore calling stored procedure
If your stored procedure returns a result set, use FromSqlRaw:
var students = context.Students.FromSqlRaw("EXEC GetStudents @FirstName", new SqlParameter("@FirstName", "John")).ToList();
//---------------------------------------------------------------
If your stored procedure modifies data (INSERT, UPDATE, DELETE), use ExecuteSqlRaw:
context.Database.ExecuteSqlRaw("EXEC UpdateStudentMark @StudentId, @NewMark", new SqlParameter("@StudentId", 1), new SqlParameter("@NewMark", 90));
//---------------------------------------------------------------
If your 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; }
}
var students = context.StudentDTOs.FromSqlRaw("EXEC GetStudentDetails").ToList();
//-------------------------------------------------------------------------------