如何使用Entity Framework Core 2.0调用存储过程? C#中的Asp.net核心2.0站点
答案 0 :(得分:2)
csproj文件:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0" />
</ItemGroup>
</Project>
的Program.cs:
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using Microsoft.EntityFrameworkCore;
namespace EfExperiment
{
class Program
{
public class Record
{
public int Id { get; set; }
public DateTime Date { get; set; }
public decimal Value { get; set; }
}
public class RecordsDbContext : DbContext
{
public DbSet<Record> Records { get; set; }
public List<Record> RecordsByDateRange(DateTime fromDate, DateTime toDate)
{
var fromDateParam = new SqlParameter("fromDate", fromDate);
var toDateParam = new SqlParameter("toDate", toDate);
return Records
.FromSql("EXECUTE dbo.RecordsByDateRange @fromDate, @toDate", fromDateParam, toDateParam)
.ToList();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=RecordsDb;Trusted_Connection=True;");
}
}
static void Main(string[] args)
{
using (var dbc = new RecordsDbContext())
{
var records = dbc.RecordsByDateRange(DateTime.Now.AddMonths(-1), DateTime.Now);
Console.WriteLine($"{records.Count} records loaded");
foreach (var record in records)
{
Console.WriteLine($"[{record.Id}] [{record.Date}] {record.Value}");
}
}
Console.WriteLine("Press <enter>");
Console.ReadLine();
}
}
}