maartenba / MvcSiteMapProvider

An ASP.NET MVC SiteMapProvider implementation for the ASP.NET MVC framework.
Microsoft Public License
537 stars 220 forks source link

The EntityType 'MyProjectName.Models.MyTable' does not declare a navigation property with the name 'Url' #425

Closed mspace23 closed 8 years ago

mspace23 commented 8 years ago

hello thank you for this tutorial. I getting this error Additional information: A specified Include path is not valid. The EntityType 'MyProjectName.Models.MyTable' does not declare a navigation property with the name 'Url'.

My class code:

 ` public class MyDynamicNodeProvider
   : DynamicNodeProviderBase
   {
    db mydbcs = new Mydb();
    public override IEnumerable<DynamicNode> GetDynamicNodeCollection(ISiteMapNode node)

    {
        // Build value 
        var returnValue = new List<DynamicNode>();

        // Create a node for each album 
        foreach (var article in mydbcs.SiteContents.Include("Url"))
        {
            DynamicNode enode = new DynamicNode();
            enode.Title = article.ArticleTitle;
            enode.ParentKey = "Url_" + article.ArticleUrl;
            enode.RouteValues.Add("id", article.ArticleID);

            returnValue.Add(enode);
        }

        // Return 
        return returnValue;
    }
}`

what type of data should I have at foreach (var article in mydbcs.SiteContents.Include("Url"))

NightOwl888 commented 8 years ago

Have a look at the documentation for IQueryable.Include, and these examples.

Include is expecting the name of a navigation property which will effectively join 2 tables together. You are specifying Url as that property. However, your entity does not have a navigation property named Url. Note the following example.

public class Article
{  
    public int ArticleId { get; set; }  
    public string ArticleTitle { get; set; }  

    public virtual ICollection<Url> Url { get; set; }  
}

public class Url
{
    public int UrlId { get; set; }
    public string ArticleUrl { get; set; }
}

Note that you only need to join tables if the Url field is in a different table than SiteContents. Most likely in this scenario your ArticleUrl is in the Article table, so you probably don't need to do this.

I am closing this question because it is not about MvcSiteMapProvider, it is about Entity Framework. If you have any other questions about Entity Framework, I suggest you post them on StackOverflow.

mspace23 commented 8 years ago

ok thank you for your reply. I have another issue now I have post it at stackoverflow. Could you help out? [http://stackoverflow.com/questions/35622970/dynamic-sitemap-from-database-doesnt-display-the-nodes] thank you again