EntityFramework.TypedOriginalValues icon indicating copy to clipboard operation
EntityFramework.TypedOriginalValues copied to clipboard

EF.Core 2.0 If model has non virtual complex type property сonfigured with key "OwnsOne"

Open SergeyIlyin opened this issue 8 years ago • 1 comments

I modifed test project to use complext type in test modelBuilder.Entity<Person>().OwnsOne(u => u.Widget);

    public class Person
    {
        [Key]
        public virtual Int64 Id { get; private set; }
        ...
        public  Widget Widget { get; set; }
        ...
    }

System.InvalidOperationException : The property 'Widget' on entity type 'Person' is being accessed using the 'Property' method, but is defined in the model as a navigation property. Use either the 'Reference' or 'Collection' method to access navigation properties.

SergeyIlyin avatar Aug 24 '17 10:08 SergeyIlyin

I am not sure if this is related to your problem, but I got the same error message today. While searching for a solution, stumbled upon this issue. I solved my problem and I am posting my findings here in case someone else stumbles here also.

public class Person
    {
        public long Id { get; private set; }
        public  Widget Widget { get; set; }
    }

public class Context : DbContext
{
    ...
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
          modelBuilder.Entity<Person>.OwnsOne(x => x.Widget);
    }
}

I was trying to optimize the SQL generated by EF, and wrote something like this :

context.Entry(Person).Property(x => x.Widget).IsModified = false;

According to the error I got, replacing "Property" with "Reference" solved my problem.

context.Entry(Person).Reference(x => x.Widget).IsModified = false;

firatakyildiz avatar Jun 25 '18 14:06 firatakyildiz