I have table with owned entity as below:
public class Audio
{
public Guid Id { get; }
public VoiceInfo { get; }
}
public class VoiceInfo
{
public VoiceProvider Provider { get; }
public Language Language { get; }
public Gender Gender { get; }
public string Name { get; }
}
with mapping:
builder.OwnsOne(x => x.VoiceInfo, v =>
{
v.Property(x => x.Gender).HasColumnName(nameof(VoiceInfo.Gender));
v.Property(x => x.Language).HasColumnName(nameof(VoiceInfo.Language));
v.Property(x => x.Name).HasColumnName(nameof(VoiceInfo.Name));
v.Property(x => x.Provider).HasColumnName(nameof(VoiceInfo.Provider));
});
When I create instance of Audio passing VoiceInfo I call
DbContext.AddAsync(audio);
then
DbContext.SaveChanges()
throws an exception with message:
...Cannot insert the value NULL into column 'Language'...
What is more interesting after call DbContext.AddAsync(audio) the VoiceInfo property from Audio instance is set to NULL. This means that exception is valid but why after adding audio instance into context owned propety is set to null ?
I added some check before and after calling AddAsync() using:
var entry = Entry(audio);
var entityState = entry.State;
var ownedPropertyState = entry.Reference(x => x.VoiceInfo).TargetEntry.State;
Before:
Both entityState and ownedPropertyState are detached which is valid (?) - they do not exists from context perspective.
After:
entityState is Added
ownedPropertyState throws and exception: Object reference not set to an instance of an object.'