1

I am using EF 6 in Visual Studio 2013. I want to get matching records from a Parent table on behalf of foreign key from Child table. I have following line of code

var record = db.ChannelFees.Include(x =>x.SubSource).ToList();

Here ChannelFees is the child table in which SubSourceId is foreign key from SubSource(Parent Table).

Channel Fee Class Looks Like:

    using System;
    using System.Collections.Generic;

    public partial class ChannelFee
{
    public virtual SubSource SubSource { get; set; }
    public int SubSource_id { get; set; }
    public double Fee { get; set; }
    public int Id { get; set; }
}

and the SubSource Class

using System;
    using System.Collections.Generic;

    public partial class SubSource
    {
        public int Id { get; set; }
        public string Description { get; set; }
        public string MapName { get; set; }
    }

But I am getting the following exception.

A specified Include path is not valid. The EntityType 'FinancialManagmentModel.ChannelFee' does not declare a navigation property with the name 'SubSource'.

What is wrong with it?

7
  • 1
    Yes LINQ to Entitites I have edited the question too. Commented Dec 9, 2015 at 6:25
  • 1
    Could you send ChannelFee and SubSource classes please? Also the mapping configuration if you are using fluent interface mapping. Commented Dec 9, 2015 at 7:10
  • 1
    I have updated the question with ChannelFee and SubSource classes @bubi Commented Dec 9, 2015 at 7:18
  • 1
    I am new to Entity Framework so I actually don't know what else has to be done to get this Commented Dec 9, 2015 at 7:19
  • Your question states that you do have two partial classes called SubSource! is that typo? Commented Dec 9, 2015 at 7:20

4 Answers 4

2

I think it should be:

public virtual ICollection<SubSource> SubSource;
Sign up to request clarification or add additional context in comments.

1 Comment

'The modifier 'virtual' is not valid for this item' Following error is occuring on placing virtual keyword
2

You could try with this:

public partial class ChannelFee
{
    public virtual ICollection<SubSource> SubSource { get; set; } // Just to enable lazy load
    public int Id { get; set; }
    public string Description { get; set; }
    public string MapName { get; set; }
}

public partial class SubSource
{
    public int Id { get; set; }
    public string Description { get; set; }
    public string MapName { get; set; }
    public virtual ChannelFee ChannelFee {get; set; } // Navigation property
}

And usually I also add a Mapping in the context (you can achieve the same result with configuration attributes)

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<SubSource>().HasRequired(t => t.ChannelFee)
            .WithMany(t => t.SubSource);
}

1 Comment

Where to put the last method (OnModelCreating())?
1

Simplest way to solve the problem is to declare a navigation property with the name 'SubSource'. through Annotations

public partial class ChannelFee
{
        [ForeignKey("SubSource_id")]  
        public virtual SubSource SubSource { get; set; }        
        public int SubSource_id { get; set; }     

}

Comments

0

I have resolved the issue at my own. Actually the EF was not generating navigation property in the Model on the behalf of Foreign Key Constraint which I created in SQL, that is why error was occuring. So I simply deleted the Model and added it again. Problem was solved.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.