Question

I have a table 'Comments' that I want to store lists of comments with dates on. These comments can belong to three different classes each one with its own table on the db. My first attempt was doing this:

public class Comment
    {
      public int CommentID { get; set; }
      public virtual int OwnerID { get; set; }
      public string Description { get; set; }        
    }

But I cant figure out how to use it with three different classes.

My next option was:

public class Comment
    {
      public int CommentID { get; set; }
      public virtual IAcceptComment Owner { get; set; }
      public string Description { get; set; }        
    }

And I assign the interface to three classes but I get an error trying to configure the context for one of the entities: (An explicit conversion exists (are you missing a cast?))

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Event>().HasMany<Comment>(e => e.Comments).WithMany(c =>       c.Owner);            
    }

I have used Model First to see how EF generates the classes and this is what it does:

public class Comments
    {

    public int Id { get; set; }
    public string Comment { get; set; }

    public virtual ICollection<Consultant> Consultant { get; set; }
    public virtual ICollection<Event> Event { get; set; }
    public virtual ICollection<Company> Company { get; set; }

}

Isn't there any way to avoid the creation of Navigation Properties? Can you think of any other approximation to the problem?

Thanks.

Was it helpful?

Solution

I think you can use Inheritance in EF

Inheritance Refernece

TPT Inheritance Usage msdn

TpT Usages

This will help you to avoid navigation properities

make Query like this

     'var db = new MySchoolEntities();

        foreach (var department in db.Departments)
        {
            Console.WriteLine();
            Console.WriteLine("The {0} department has the following courses:",
                        department.Name);

            Console.WriteLine();
            Console.WriteLine("   All courses");

            foreach (var course in department.Courses)
            {
                Console.WriteLine(" The Course Name {0}", course.Title);
            }

            Console.WriteLine();

            if (department.Courses.OfType<OnlineCourse>().Count() > 0)
            {
                Console.WriteLine("   Online courses are");
                foreach (var online in department.Courses.OfType<OnlineCourse>())
                {

                    Console.WriteLine("Online Course is {0} & Link : {1}    ", online.Title, online.URL);

                }
            }

            Console.WriteLine();

            Console.WriteLine("   Onsite courses are");

            foreach (var onsite in department.Courses.OfType<OnsiteCourse>())
            {
                Console.WriteLine(" Online Course Venue : {0}    ", onsite.Location);
            }


        }

        Console.ReadLine();

Hope this will make you avoid lengthy Navigations.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top