My constructor sets the attribute, so why is EFcore complaining?
why is EFCore complaining?
Required member 'UpMedia.OwnerId' must be set in the object initializer or attribute constructor.
Required member 'UpMedia.Owner' must be set in the object initializer or attribute constructor.
Here:
var upMedia = new UpMedia(upMediaDto.Type, user, upMediaDto.Bucket, upMediaDto.Path);
My code is
???????15 Replies
please try it like this:
of course, you can initialize all the properties in the same way like the
UpMedia
property
in fact, the complains go from the C# itself as you have to use object initializer to initialize the properties here.you have to use object initializer to initialize the properties herebeg your pardon what is object initalizer the empty constructor?
no, this is the part in those
{}
brackets:
var somevar = new MyClass(... here goes constructor args...) { someProperty = someInitialValueForRequiredProperty, otherProperty = otherValue}
here are details: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/object-and-collection-initializers
Object and Collection Initializers - C#
Object initializers in C# assign values to accessible fields or properties of an object at creation after invoking a constructor.
Nothing about this question has anything to do with EFCore
This is a class used in EFCore, but the compiler is the one telling you about the issue
The error means you must set all properties and fields that require a value and can't be null. These are
OwnerId
and Owner
This one doesn't set them
Maybe you are confused because you think base()
calls the other constructor. It does not. You'd use this()
So, either fix this constructor and set the values, or giveOwnerId
and Owner
a value.
Alternatively, use null-forgiving operators and tell the compiler the value can be null
.
Note that the values will be null
but the compiler won't tell you. ONLY use this if you are aware that they will be null in the context, and/or they are given a value soon after.
Also, this doesn't solve anything
Oh, nevermind. I misunderstand the questionit solves the problem when the
required
is used, I don`t take care about the background why this is set in this way in EF entity.@surwren when you have
required
on properties you must set them in the object initializer. Remove those
You already have constructors. Use one or the other
Considering this is an EF class you might just want to remove the constructors instead because EF works easier with required properties rather than constructorsvar upMedia = new UpMedia(upMediaDto.Type, user, upMediaDto.Bucket, upMediaDto.Path);
does this not explicitly set the required variables?No, the constructor is unrelated to the
required
keyword
Because when a constructor is made, it has its own requirements by default
required
is purely for object initializers
it sets the owner and ownerid explicitly
i dont get it
what is the difference between constructor and object initializer
or do you mean this?
but im not even using that
:harold:
i dont understand
i thought required makes EFcore deduce that this object is dependent on the other one
or something similar
meaning that one User can have 0 or many UpMedia
but every UpMedia must have one user
So remove
required
That's what I said
If you don't use an object initializer required
is useless
No, EFCore doesn't care
It checks nullabillityYou're probably thinking of the
Required
attribute. But FsQyou is right, since C# 8 (.NET Core 3.1) EF can just use nullability directlySee https://learn.microsoft.com/en-us/ef/core/modeling/entity-properties?tabs=data-annotations%2Cwithout-nrt#required-and-optional-properties and https://learn.microsoft.com/en-us/ef/core/miscellaneous/nullable-reference-types
Working with nullable reference types - EF Core
Working with C# nullable reference types when using Entity Framework Core
Entity Properties - EF Core
How to configure and map entity properties using Entity Framework Core
I saw the [Required] annotation in a couple places before and didn't really understand the difference from the required keyword
Need to research that after I get stuff done
They're not related