How to set IsModified to false on a navigation property

asp.net-core c# entity-framework-core

Question

I have Article and ApplicationUser model classes:

public class ApplicationUser
{
    ...

}

public class Article
{
    ...

    public ApplicationUser CreatedBy { get; set; }
}

I have tried to set CreatedBy property to false this way:

base.Entry(entity).Property(x => x.CreatedBy).IsModified = false;

But I get this error:

The property 'CreatedBy' on entity type 'ApplicationUser' 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.

1
0
9/17/2018 7:29:33 AM

Accepted Answer

If I understand correctly , the Article entity may looks like :

public class Article
{
    public int Id { get; set; }

    public string UserID { get; set; }

    // ...

    [ForeignKey("UserID")]
    public ApplicationUser CreatedBy { get; set; }
}

As the error info described , the CreatedBy is a navigation property here .

So change your code to

Entry(entity).Reference(x => x.CreatedBy).IsModified = false; ,

It may work as expected .

3
9/17/2018 7:52:10 AM

Popular Answer

I changed to access the CreatedBy using the 'Reference' method insted of 'Property' method:

base.Entry(entity).Reference(x => x.CreatedBy).IsModified = false;


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow