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