wanjydan
wanjydan

Reputation: 139

How to set IsModified to false on a navigation property

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.

Upvotes: 2

Views: 4795

Answers (2)

itminus
itminus

Reputation: 25380

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 .

Upvotes: 10

wanjydan
wanjydan

Reputation: 139

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

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

Upvotes: 1

Related Questions