15

I'm using EF Core with ASP Core 2.0. Using latest Identity framework. I get this exception on page All.

InvalidOperationException: The property 'User' is not a navigation property of entity type 'Gallery'. The 'Include(string)' method can only be used with a '.' separated list of navigation property names.

ApplicationUser looks like:

public class ApplicationUser : IdentityUser<Guid>
{
    public ICollection<Gallery> Galleries { get; set; }
}

Entity Gallery looks like:

public class Gallery
{
    public int Id { get; set; }
    public Guid UserId { get; set; }
    public string Title { get; set; }
    public int? ArticleId { get; set; }
    public string Photos { get; set; }
    public DateTime CreatedAt { get; set; }
    public DateTime UpdatedAt { get; set; }

    public Article Article { get; set; }
    public ApplicationUser User { get; set; }

    [NotMapped]
    public List<string> PhotosList
    {
        get { return Photos?.Split('|').ToList(); }
        set { Photos = string.Join("|", value); }
    }
}

Controller for View looks like:

public async Task<IActionResult> All()
    {
        var databaseContext = db.Galleries.Include(x => x.Article).Include(x => x.User);

        return View(await databaseContext.ToListAsync());
    }

I have no idea why it dont crash on Article..

Database is up-to-date.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
user1085907
  • 1,009
  • 2
  • 16
  • 40
  • Does [this](https://stackoverflow.com/questions/36047948/ef-the-navigation-property-is-not-a-declared-property-on-type) help? – kabanus Oct 19 '17 at 20:29
  • kabanus: nope, foreignkey didnt help – user1085907 Oct 19 '17 at 20:37
  • Cristic: well I dont have option x.ApplicationUser cause I defined navigation named User not ApplicationUser.. ApplicationUser is class not variable name – user1085907 Oct 19 '17 at 20:38
  • Worth noting. I made a mistake in my scenario. I was using Database.Order.Include(o => o.ShopId) when in fact it should have been Database.Order.Include(o => o.Shop) - literally a typo in my case - just check you are not accidentally linking to an Id as you would in a SQL query. – Andy Jul 21 '21 at 07:38

2 Answers2

2

add a ForeignKey attribute

using System.ComponentModel.DataAnnotations.Schema;

...

[ForeignKey("Article")]
public int? ArticleId { get; set; }

[ForeignKey("User")]
public Guid UserId { get; set; }

You can also put the attribute on the navigation property

[ForeignKey("UserId")]
public ApplicationUser User { get; set; }

Also, make sure your dbContext inherits from IdentityDbContext<ApplicationUser, ...>

2

You can run into this if you manually add extra properties to Models.

To troubleshoot it, run SQL Profiler and capture the RAW SQL, execute the SQL against the database and see why the query doesn't work, ie which property 'x' is not a navigation property of entity type 'y'.

Then go to the model and remove the extra property you added manually.

PS: If you don't have a SQL dB you can use another profiler. Alternatively just check the diffs in source control.

halfer
  • 19,824
  • 17
  • 99
  • 186
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • This is definitely the root cause, but manually added properties are also necessary to programs. A better solution will be to detect if the property is part of the underlying model (IE a boolean result telling us if the property is valid for LINQ-to-SQL). The error already tells us which property it doesn't like. – Tim May 11 '21 at 18:47