【问题标题】:Windows 10 uwp - Entity Core Sqlite : Include query errorWindows 10 uwp - Entity Core Sqlite:包括查询错误
【发布时间】:2017-07-30 16:31:01
【问题描述】:

在 Windows 10 UWP 应用程序中,我正在使用带有 Entity Framework Core 1.1 的本地 sqlite 数据库,除此之外它现在工作正常:

我正在尝试使用简单的 linq 查询将相关对象包含到我的用户对象中。我测试了一些查询,但是带有 include 方法和 select 限制的 lambdas 不起作用。

代码示例:

public async Task<IUser> GetAsync(Guid id)
{
    // Working, but useless
    var user = await this.DbSet.Include(u => u.ProfileNavigation).FirstOrDefaultAsync();

    // Returns null
    var user1 = await this.DbSet.Include(u => u.ProfileNavigation).FirstOrDefaultAsync(u => u.IdGuid == id);

    // Returns null
    var user2 = await this.DbSet.Where(u => u.IdGuid == id).Include(u => u.ProfileNavigation).FirstOrDefaultAsync();

    // Returns null linked object
    User user3 = await this.DbSet.SingleOrDefaultAsync(u => u.IdGuid == id);
    await this.Context.Entry(user3).Reference(p => p.ProfileNavigation).LoadAsync();

    // Result valid but painfull
    User res = await (from u in this.DbSet
                      join p in this.Context.Set<Profile>() on u.ProfileGuid equals p.IdGuid
                      where u.IdGuid == id
                      select new User
                      {
                          Id = u.Id,
                          ProfileId = u.ProfileId,
                          ManagerUserId = u.ManagerUserId,
                          RegionId = u.RegionId,
                          Number = u.Number,
                          Password = u.Password,
                          FirstName = u.FirstName,
                          LastName = u.LastName,
                          Email = u.Email,
                          HideOnBoardAppConstr = u.HideOnBoardAppConstr,
                          ModifiedByUserId = u.ModifiedByUserId,
                          ProfileNavigation = p
                      }).FirstOrDefaultAsync();

    return res;
}

有人遇到过这个问题并解决了吗?

提前致谢。

【问题讨论】:

    标签: c# entity-framework linq sqlite uwp


    【解决方案1】:

    有一个关于create Database by the Entity Framework的官方文档。

    在本演练中,您将构建一个通用 Windows 平台 (UWP) 应用程序,该应用程序使用 Entity Framework 对本地 SQLite 数据库执行基本数据访问。

    您应该能够检查是否包含 BlogPost 类作为示例的 BloggingContext 类。

    还有一个Entity Framework Loading Related Entities的文档。

    急切加载是对一种实体类型的查询同时加载相关实体作为查询的一部分的过程。急切加载是通过使用 Include 方法实现的。例如,下面的查询将加载博客以及与每个博客相关的所有帖子。

    例如:

    BloggingContext 类:

    public class BloggingContext : DbContext
    {
        public DbSet<Blog> Blogs { get; set; }
        public DbSet<Post> Posts { get; set; }
    
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlite("Data Source=blogging.db");
        }
    }
    
    public class Blog
    {
        public int BlogId { get; set; }
        public string Url { get; set; }
    
        public List<Post> Posts { get; set; }
    }
    
    public class Post
    {
        public int PostId { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }
    
        public int BlogId { get; set; }
        public Blog Blog { get; set; }
    }
    

    包含查询代码:

    using (var db = new BloggingContext())
    {
        var blogs1 = db.Blogs.Include(b => b.Posts).ToList();
    }
    

    【讨论】:

      【解决方案2】:

      通过为每个请求使用数据库上下文实例可以部分解决此问题。该问题与上下文的依赖注入有关(我尝试使用 SimpleIoc 和 Microsoft.Extensions.DependencyInjection),但它仍然让我感到困惑。

      欢迎任何cmets。

      【讨论】:

        猜你喜欢
        • 2016-04-23
        • 2016-01-24
        • 1970-01-01
        • 2020-09-25
        • 2016-12-08
        • 2017-10-29
        • 1970-01-01
        • 2016-09-03
        • 1970-01-01
        相关资源
        最近更新 更多