Efcore: „OwnsOne“ no longer works with Verion 3.0.0-preview7.19362.6

Created on 26 Jul 2019  Â·  2Comments  Â·  Source: dotnet/efcore

I have read in various posts that there are problems with "OwnsOne". Will this problem be fixed in one of the next versions or will I have to continue to work with version 2.6 forever or do I have to completely rebuild large parts of the program?
The example works fine in 2.6. The data is stored and when reading the data from the database, the program aborts with the following error:

System.NullReferenceException: "Object reference not set to an instance of an object."

Many thanks.
Jörg

using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    public class JgLogDb : DbContext
    {
        public string SqlVerbindung = @"Data Source=.\SqlExpress;Initial Catalog = JgTest; Integrated Security = True";

        public DbSet<TabTest1> TabTest1Set { set; get; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<TabTest1>().OwnsOne(p => p.MTest);
            base.OnModelCreating(modelBuilder);
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(SqlVerbindung);
        }
    }

    public class TabTest1
    {
        public Guid Id { get; set; } = Guid.NewGuid();
        public string Feld1 { get; set; }
        public MyTest MTest { get; set; }
    }

    [Owned]
    public class MyTest
    {
        public string FeldTest { get; set; }
    }

    class Program
    {
        static async Task Main()
        {
            using (var db = new JgLogDb())
            {
                await db.TabTest1Set.AddAsync(new TabTest1()
                {
                    Feld1 = "Hallo",
                    MTest = new MyTest()
                    {
                        FeldTest = "Ballo"
                    }
                });
                await db.SaveChangesAsync();
            }

            using (var db = new JgLogDb())
            {
                var liste = await db.TabTest1Set
                    .Select(s => new
                    {
                        f1 = s.Feld1,
                        f2 = s.MTest.FeldTest
                    }).ToListAsync();

                foreach (var ds in liste)
                    Console.WriteLine($"{ds.f1}  {ds.f2}");
            }

            Console.ReadLine();
        }
    }
}
closed-fixed customer-reported type-bug

Most helpful comment

I'm getting this error while trying to filter via owned entities (FirstOrDefault, Where, ...).

All 2 comments

I'm getting this error while trying to filter via owned entities (FirstOrDefault, Where, ...).

This works with the currently nightly build--output is "Hallo Ballo".

Was this page helpful?
0 / 5 - 0 ratings