检索标量函数的值

发布于 2025-01-10 20:18:54 字数 3187 浏览 1 评论 0原文

目标

当您通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若言繁花未落 2025-01-17 20:18:54

Luuk 的回答

var x = ss.Persons.Where(x => x.PersonId < testDBContext.FuncTest()).ToList();

实体框架核心,UD sql 函数抛出 System.NotImplementedException:“该方法或操作未实现。”

Answer from 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.'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文