user3079834
user3079834

Reputation: 2214

UWP Sqlite one-to-many relation

I'm trying to use SQLite with the new EF Core and have been following the tutorial. Before I used SQLite too, but I used an extra DB-class with references to the real class. e.g. below Blog_DB contains the ID to the BlogImage (another class) and then I used to create the real Blog class, that loads the BlogImage class into Blog.BlogImage.

public class Blog_DB
{
public int BlogId { get; set; }
public string Url { get; set; }

public int BlogImageID { get; set; }
}

public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }

public BlogImage BlogImage { get; set; }
}

public class BlogImage
{
    public int BlogImageId { get; set; }
    public byte[] Image { get; set; }
    public string Caption { get; set; }
}

However now I heard about EF Core and it's one-to-one and one-to-many relation, which is done the following way:

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }

    public BlogImage BlogImage { get; set; }
}

public class BlogImage
{
    public int BlogImageId { get; set; }
    public byte[] Image { get; set; }
    public string Caption { get; set; }

    public int BlogId { get; set; }
    public Blog Blog { get; set; }
}

But I do not understand, how this works (or I did not understand/read the documentation right) as when I load Blogs the Entry BlogImage is empty.

using (var db = new BloggingContext())
{
    var m = db.Blogs.FirstOrDefault(); // the first entry
    m.BlogImage = new BlogImage { Caption = "My Caption" }; // does not work
    db.BlogImage.Add(new BlogImage { Blog = m, BlogId = m.BlogId, Caption = "My Caption" }); // doesn't work either
    db.SaveChanges();                
}

Upvotes: 0

Views: 500

Answers (1)

Jerry Li
Jerry Li

Reputation: 1042

when I load Blogs the Entry BlogImage is empty.

In Entity Framework, an entity can be related to other entities through an association (relationship). And the Navigation properties provide a way to navigate an association between two entity types, and loading the related Navigation properties is achieved by use of the Include method. As the official document for EF Core is not complete, here is the documentation about Loading Related Entities for EF6.x.

In this case, entity BlogImage is a navigation property for entity Blog. So using db.BlogImage.Add(new BlogImage { Blog = m, BlogId = m.BlogId, Caption = "My Caption" }); to add BlogImage does work, but you should use Include method to load entity BlogImage for Blog.

Following is the code I have verified:

using (var db = new BloggingContext())
{
    // load the Blog without BlogImage
    var blogWithoutBlogImage = db.Blogs.FirstOrDefault();//blogWithoutBlogImage.BlogImage is null

    // load the Blog inclue the BlogImage
    var blogWithBlogImage = db.Blogs.Include(x => x.BlogImage).FirstOrDefault();
    var blogImage = blogWithBlogImage.BlogImage;
}

Upvotes: 1

Related Questions