【问题标题】:Getting processed data after stored procedure call存储过程调用后获取处理后的数据
【发布时间】:2020-06-07 01:35:28
【问题描述】:
//1. inserting data into the db
_context.MyModel.AddRange(content);
_context.SaveChanges();

//2. calling a stored procedure for data process
_context.Database.ExecuteSqlRaw("CALL process_data()");

//3. getting back processed data - not working, getting pre-processed data instead
List<MyModel> processedContent = _context.MyModel.ToList();

我在从第 2 步中获取已处理的数据时遇到问题。在第 3 步中。-我不断返回的数据是我在第 1 步中插入的数据。(预处理数据),而我想获取存储过程处理过的数据。

我在第 3 步设置了一个断点,并在 db 上运行查询 - 是的,所有已处理的数据都在那里。 _context 似乎没有更新新数据。

知道我在这里做错了什么吗?

【问题讨论】:

  • 你试过"var results = _context.Database.ExecuteSqlRaw("CALL process_data()");"
  • 尝试 _context.SaveChanges();执行存储过程后的方法。
  • @Tan ExecuteSqlRaw 查询只返回受影响的行数作为 int。 @ParamjotSingh 谢谢,但我试过了,它没有解决问题。在我创建了一个新的数据库上下文来获取数据后,我得到了处理后的数据。所以这增加了我的理论,即_context 需要刷新。
  • 你可以尝试添加一个调试点 tehre 并检查是否有更多的数据然后只是一个 int。
  • @Tan 它只返回一个整数:docs.microsoft.com/en-us/dotnet/api/…

标签: c# postgresql asp.net-core entity-framework-core


【解决方案1】:

我现在唯一的解决方案是实例化一个新的上下文:

//1. inserting data into the db
_context.MyModel.AddRange(content);
_context.SaveChanges();

//2. calling a stored procedure for data process
_context.Database.ExecuteSqlRaw("CALL process_data()");

MyDbContext _newContext = new MyDbContext(_configuration);

//3. getting back processed data - working fine with new db context
List<MyModel> processedContent = _newContext.MyModel.ToList();

另一种解决方案是使用以下方法刷新_context 中的每个条目:

foreach (var c in content)
{
   _context.Entry(c).Reload();
}

这可行,但对性能不友好。

【讨论】:

    【解决方案2】:

    我无法重现该问题,请参阅下面的代码。尽管 EF Core 的 DbContext 确实保留了状态,但每次执行 ToList(或几乎任何其他查询操作)时,都会执行您的查询并获取最新结果。一个值得注意的例外是Find,它将返回跟踪的实体实例而不查询数据库。

    如果您仍然有问题,请尝试更改下面的完整示例,以演示什么不起作用。

    class Program
    {
        static async Task Main(string[] args)
        {
            await using var ctx = new BlogContext();
            await ctx.Database.EnsureDeletedAsync();
            await ctx.Database.EnsureCreatedAsync();
    
            await ctx.Database.ExecuteSqlRawAsync(@"
    CREATE PROCEDURE process_data()
        LANGUAGE SQL
    AS $$
        INSERT INTO ""Blogs"" (""Name"") VALUES ('Inserted from stored procedure');
    $$;
    ");
    
            ctx.Blogs.Add(new Blog { Name = "Original" });
            await ctx.SaveChangesAsync();
    
            ctx.Database.ExecuteSqlRaw("CALL process_data()");
    
            await foreach (var blog in ctx.Blogs)
                Console.WriteLine(blog.Name);
        }
    }
    
    public class BlogContext : DbContext
    {
        public DbSet<Blog> Blogs { get; set; }
    
        static ILoggerFactory ContextLoggerFactory
            => LoggerFactory.Create(b => b.AddConsole().AddFilter("", LogLevel.Information));
    
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            => optionsBuilder
                .UseNpgsql(@"...")
                .EnableSensitiveDataLogging()
                .UseLoggerFactory(ContextLoggerFactory);
    }
    
    public class Blog
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-10-14
      • 1970-01-01
      • 1970-01-01
      • 2014-06-23
      • 2020-01-09
      • 1970-01-01
      • 1970-01-01
      • 2012-01-16
      • 2013-05-26
      相关资源
      最近更新 更多