Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Owned property returned as null when all of its properties are null #20882

Closed
ErroneousFatality opened this issue May 7, 2020 · 5 comments
Closed

Comments

@ErroneousFatality
Copy link

ErroneousFatality commented May 7, 2020

Inside my entity Submission:

public class Submission
{
   public string Id { get; private set; }
   ...
   public ObjectData ObjectData {get; private set;}
   ...
}

I have an owned entity of type ObjectData:

public class ObjectData
{
    // Properties
    public byte? CategoryType { get; private set; }
    public decimal? Cost { get; private set; }

    // Constructors
    public ObjectData(
        byte? objectCategoryType,
        decimal? objectCost
    )
    {
        CategoryType = objectCategoryType;
        Cost = objectCost;
    }

    private ObjectData() { }
}

Their relationship is configured inside the Context.OnModelCreating like so:

modelBuilder.Entity<Submission>().OwnsOne<ObjectData>(e => e.ObjectData);

When a Submission = { ObjectData = { ObjectCategoryType = null, ObjectCost = null } } is persisted, and then later fetched from the database, it will return as a Submission = { ObjectData = null }.

I expected it to return as Submission = { ObjectData = { ObjectCategoryType = null, ObjectCost = null } }.

It seems that EFCore determines if the owned entity is null by checking if all of its columns have null values, instead of differentiating between an "empty" owned property and a null owned property.

I need the ObjectData to always exist and all of its properties to be nullable. Any ideas on how to remedy this?

Further technical details

EF Core version: 3.1.3
Database provider: Microsoft.EntityFrameworkCore.SqlServer v3.1.3
Target framework: .NET Framework 4.8 / .NET Framework 3.1

@smitpatel
Copy link
Contributor

By design, see #9005 (comment)

@ErroneousFatality
Copy link
Author

@smitpatel Your answer does not help me solve my issue.
What would be the designed EFCore solution for my usecase?

@smitpatel
Copy link
Contributor

If you need to always create the owned referenced regardless of property values, hence making the dependent required see #12100

@ErroneousFatality
Copy link
Author

ErroneousFatality commented May 7, 2020

For anyone coming here with a similiar problem, I made a simple workaround, until #12100 feature is released.
I added a dummy bool property to the owned entity class, which I always set to a non-null value. That way, my owned entity never has all null values, and is thus never returned as null from the DB.

@sergey-gindin
Copy link

I had same problem in my project. I just have defined default value in my private constructor. It was suitable for me.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants