question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

GetDatabaseValues for owned type throws exception if the type is only used by one entity

See original GitHub issue

Calling EntityEntry.GetDatabaseValues() on an owned type can raise an exception in certain circumstances.

Steps to reproduce

  1. Run this simplified project that is only using the Order and ShipmentAddress entities: https://github.com/coder925/EntityFramework.Docs/tree/owned-type-issue/samples/core/Modeling/OwnedEntities

Expected result: Only the changed properties of the owned entity is saved. Actual result: An exception:

System.InvalidOperationException
  HResult=0x80131509
  Message=Cannot create a DbSet for 'StreetAddress' because it is configured as an owned entity type and should be accessed through the defining entities.
  Source=Microsoft.EntityFrameworkCore
  StackTrace:
   at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityType()
   at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.CheckState()
   at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityQueryable()
   at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.System.Linq.IQueryable.get_Provider()
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.AsNoTracking[TEntity](IQueryable`1 source)
   at Microsoft.EntityFrameworkCore.Internal.EntityFinder`1.GetDatabaseValuesQuery(InternalEntityEntry entry)
   at Microsoft.EntityFrameworkCore.Internal.EntityFinder`1.GetDatabaseValues(InternalEntityEntry entry)
   at Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry.GetDatabaseValues()
   at EFModeling.OwnedEntities.Program.Main(String[] args) in C:\repos-github\EntityFramework.Docs\samples\core\Modeling\OwnedEntities\Program.cs:line 46

Workaround: Uncomment code in OwnedEntityContext:

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            #region OwnsOne
            modelBuilder.Entity<Order>().OwnsOne(p => p.ShippingAddress);
            #endregion

            // Uncomment following line and it starts working
            #region OwnsOne
            //// modelBuilder.Entity<Order2>().OwnsOne(p => p.ShippingAddress);
            #endregion


        }

It seems if more than one Entity is owning ShipmentAddress the excpetion is not occuring.

Use case

I am receiving a detached Order entity that needs to be updated in the database. However, I am only allowed to save actual changed properties and cannot use context.Update(order) .

Instead, I use this read-update pattern:

var orderEntry = context.Attach(order);
orderEntry.OriginalValues.SetValues(orderEntry.GetDatabaseValues());

and for each owned type owned by Order (i.e. value objects):

var shippingAddressEntry = context.Entry(order.ShippingAddress);
shippingAddressEntry.OriginalValues.SetValues(shippingAddressEntry.GetDatabaseValues());

Further technical details

EF Core version: 3.0.0 Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: .NET Core 3.0 Operating system: Windows 10 Enterprise IDE: Visual Studio 2019 16.3.0

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
AndriySvyrydcommented, Oct 16, 2019
0reactions
coder925commented, Oct 14, 2019

Thanks for your suggestion! Unfortunately, it gives the same exception when trying to fetch the database values on the next line:

var dbValues = shippingAddressEntry.GetDatabaseValues();
Read more comments on GitHub >

github_iconTop Results From Across the Web

Owned Entity Types - EF Core
The entity containing an owned entity type is its owner. Owned entities are essentially a part of the owner and cannot exist without...
Read more >
c# - Entity framework GetDatabaseValues() throw ...
Entity framework GetDatabaseValues() throw NullReferenceException ... The problem is that when trying to save an object (only 1 type of the ...
Read more >
Maximo 7.6 API - Mbo
Get a related MboSet for the object using the named relationship only if the the MboSet ... Throws an application exception if the...
Read more >
Should I check if something exists in the db and fail fast or ...
If not, then sure, insert away and throw an exception. ... A check of this type is simply not a reliable way prevent...
Read more >
Dealing With Optimistic Concurrency in Entity Framework
When you update a database record with the entity values, ... The rowversion data type ensures that the column holds a different value...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found