检索标量函数的值
Retrieve value of Scalar Function
目标
在使用 Entity Framework 核心使用 C# 应用程序时启用 SQL 服务器标量值函数。
问题
当我想在我的 C# 应用程序中使用 EF Core 检索 SQL 标量函数的值时,代码不起作用。
我缺少代码的哪一部分?
信息:
- .Net 核心版本 5
- 使用 C# 应用程序控制台
- 使用 EF 核心 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;
}
}
}
}
Luuk 的回答
var x = ss.Persons.Where(x => x.PersonId < testDBContext.FuncTest()).ToList();
Entity Framework Core, UD sql function throws System.NotImplementedException: 'The method or operation is not implemented.'
目标
在使用 Entity Framework 核心使用 C# 应用程序时启用 SQL 服务器标量值函数。
问题
当我想在我的 C# 应用程序中使用 EF Core 检索 SQL 标量函数的值时,代码不起作用。
我缺少代码的哪一部分?
信息:
- .Net 核心版本 5
- 使用 C# 应用程序控制台
- 使用 EF 核心 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;
}
}
}
}
Luuk 的回答
var x = ss.Persons.Where(x => x.PersonId < testDBContext.FuncTest()).ToList();
Entity Framework Core, UD sql function throws System.NotImplementedException: 'The method or operation is not implemented.'