检索标量函数的值
目标
当您通过 Entity Framework Core 使用 C# 应用程序时,能够使用 SQL Server 标量值函数。
问题
当我想在 C# 应用程序中使用 EF Core 检索 SQL 标量函数的值时,代码不起作用。
我缺少代码的哪一部分?
信息:
- .Net Core 版本 5
- 使用 C# 应用程序控制台
- 使用 EF core v5
谢谢!
代码:
CREATE DATABASE testDB;
CREATE TABLE Persons
(
PersonID int,
LastName varchar(255),
FirstName varchar(255)
);
INSERT INTO Persons (PersonID, LastName, FirstName)
VALUES (1, 'Cardinal','Tom B. Erichsen'),
(2, 'Cardinal','Jerry B. Erichsen'),
(3, 'Cardinal','Ben B. Erichsen'),
(4, 'Cardinal','James B. Erichsen');
CREATE FUNCTION dbo.FuncTest()
RETURNS int
AS
BEGIN
RETURN 5
END
C# 代码:
using System;
using System.Collections.Generic;
#nullable disable
namespace TestConsoleApp.Models
{
public partial class Person
{
public int PersonId { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
}
}
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
#nullable disable
namespace TestConsoleApp.Models
{
public partial class testDBContext : DbContext
{
public testDBContext()
{
}
public testDBContext(DbContextOptions<testDBContext> options)
: base(options)
{
}
public virtual DbSet<Person> Persons { get; set; }
[DbFunction("FuncTest", "dbo")]
public static int FuncTest()
{
throw new NotImplementedException();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer("");
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasAnnotation("Relational:Collation", "Latin1_General_CI_AS");
modelBuilder.Entity<Person>(entity =>
{
entity.HasNoKey();
entity.Property(e => e.FirstName)
.HasMaxLength(255)
.IsUnicode(false);
entity.Property(e => e.LastName)
.HasMaxLength(255)
.IsUnicode(false);
entity.Property(e => e.PersonId).HasColumnName("PersonID");
});
modelBuilder.HasDbFunction(() => FuncTest()).HasName("FuncTest").HasSchema("dbo");
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
}
using System;
using TestConsoleApp.Models;
namespace TestConsoleApp
{
public class Program
{
static void Main(string[] args)
{
using (var ss = new testDBContext())
{
var test = testDBContext.FuncTest();
int test2 = 23;
}
}
}
}
Goal
Enable to use a SQL Server scalar-valued function when you use C# application by using Entity Framework Core.
Problem
The code doesn't work when I want to retrieve the value of SQL scalar function using EF Core in my C# application.
What part of the code am I missing?
Info:
- .Net Core version 5
- Using C# application console
- Using EF core v5
Thank you!
Code:
CREATE DATABASE testDB;
CREATE TABLE Persons
(
PersonID int,
LastName varchar(255),
FirstName varchar(255)
);
INSERT INTO Persons (PersonID, LastName, FirstName)
VALUES (1, 'Cardinal','Tom B. Erichsen'),
(2, 'Cardinal','Jerry B. Erichsen'),
(3, 'Cardinal','Ben B. Erichsen'),
(4, 'Cardinal','James B. Erichsen');
CREATE FUNCTION dbo.FuncTest()
RETURNS int
AS
BEGIN
RETURN 5
END
C# code:
using System;
using System.Collections.Generic;
#nullable disable
namespace TestConsoleApp.Models
{
public partial class Person
{
public int PersonId { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
}
}
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
#nullable disable
namespace TestConsoleApp.Models
{
public partial class testDBContext : DbContext
{
public testDBContext()
{
}
public testDBContext(DbContextOptions<testDBContext> options)
: base(options)
{
}
public virtual DbSet<Person> Persons { get; set; }
[DbFunction("FuncTest", "dbo")]
public static int FuncTest()
{
throw new NotImplementedException();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer("");
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasAnnotation("Relational:Collation", "Latin1_General_CI_AS");
modelBuilder.Entity<Person>(entity =>
{
entity.HasNoKey();
entity.Property(e => e.FirstName)
.HasMaxLength(255)
.IsUnicode(false);
entity.Property(e => e.LastName)
.HasMaxLength(255)
.IsUnicode(false);
entity.Property(e => e.PersonId).HasColumnName("PersonID");
});
modelBuilder.HasDbFunction(() => FuncTest()).HasName("FuncTest").HasSchema("dbo");
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
}
using System;
using TestConsoleApp.Models;
namespace TestConsoleApp
{
public class Program
{
static void Main(string[] args)
{
using (var ss = new testDBContext())
{
var test = testDBContext.FuncTest();
int test2 = 23;
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Luuk 的回答
实体框架核心,UD sql 函数抛出 System.NotImplementedException:“该方法或操作未实现。”
Answer from Luuk
Entity Framework Core, UD sql function throws System.NotImplementedException: 'The method or operation is not implemented.'