UWP - 实体框架核心(dbcontext)访问列表子成员 [英] UWP - entity framework core (dbcontext) access to list submember
问题描述
大家好,
我遇到很多麻烦并且一直在调查很多小时,所以我完全陷入困境。
我按照页面中的教程:
UWP入门 - 新数据库 - EF Core | Microsoft Docs
这是创建一个SQLite数据库,并能够在UWP中使用EntityFrameworkCore。
直到这里一切都按预期工作。
但是......我无法使用
Hi All,
I am having a lot of trouble with this and been investigating many many hours, so I am completely stuck.
I followed the tutorial in the page:
Getting Started on UWP - New Database - EF Core | Microsoft Docs
This is to create a SQLite database and be able to use it in UWP with EntityFrameworkCore.
Until here everything works as intended.
But... I am not able to use the
public List<Post> Posts { get; set; }
当我尝试使用它时,我收到错误:
When I try to use it, I get the error:
'Object reference not set to an instance of an object.'
但是我尝试用几种方法初始化对象,并且无法使其工作。
我会说明显的方法是:
But I tried to initialize the object in several ways, and cannot make it work.
I would say that the obvious approach is this:
var blog = new Blog { Url = "theurl", Posts = new List<Posts>() };
db.Blogs.Add(blog);
var thebest = new Post{ Title = "MyTest"};
db.Blogs.SingleOrDefault(x => x.Url == "theurl").Posts.Add(thebest);
但是我处于以下循环中:
But I am in a loop of:
'Object reference not set to an instance of an object.'
我认为问题是当我使用SingleOrDefault或FirstOrDefault等等时......它会返回一个引用?而不是原来的?
在DbContext中使用这个列表的正确方法是什么?
我尝试过:
查看和查看互联网。
以多种不同方式初始化变量。
I think that the problem is when I am using SingleOrDefault or FirstOrDefault etc etc... it returns a reference? and not the original?
What is the correct way to use this list inside the DbContext?
What I have tried:
Looking and looking in the internet.
Initialize the variables in several different ways.
var blog = new Blog { Url = "theurl", Posts = new List<Posts>() };
db.Blogs.Add(blog);
var thebest = new Post{ Title = "MyTest"};
db.Blogs.SingleOrDefault(x => x.Url == "theurl").Posts.Add(thebest);
var blog = new Blog { Url = NewBlogUrl.Text };
db.Blogs.Add(blog);
db.Blogs.SingleOrDefault(x => x.Url == NewBlogUrl.Text).Posts = new List<Post>();
始终相同的异常。
Always same exception.
推荐答案
实体框架中的集合公开为 DbSet
集合。
Collections in Entity Framework are exposed as DbSet
collections.
根据这个:
According to this:
EF Core尚不支持延迟加载。您可以在我们的待办事项上查看延迟加载项目 [ ^ ]跟踪此功能。
Lazy loading is not yet supported by EF Core. You can view the lazy loading item on our backlog[^] to track this feature.
您将需要使用其中一个而是在该页面上显示的热切/显式加载选项。
You will need to use one of the eager / explicit loading options shown on that page instead.
这篇关于UWP - 实体框架核心(dbcontext)访问列表子成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!