0

I am trying to make an application where in I have made a Location model,controller and views using scaffolding. I have location name and location ID properties in location. Database is getting created and location table also and data is getting populated. Now i want to make a department class where I have 3 properties namely Dept_ID, Dept_Name and Loc_ID(Foreign key). I have added required codes in respective files as following.

in Department.cs(Model)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace DMS.Models
{
    public class Department
    {
        public int D_ID { get; set; }
        public string D_Name { get; set; }
        [ForeignKey("L_ID")]
        public int L_ID { get; set; }

    }
} 

in DB_CONTEXT class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace DMS.Models
{
    public class DB_CONTEXT : DbContext
    {
        public DbSet<Location> Movies { get; set; }
        public DbSet<Department> Department { get; set; } 
    }
}

and in locatoin.cs(Model)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DMS.Models
{
    public class Location
    {
        public int ID { get; set; }
        public string L_Name { get; set; }
    }
}

when i am trying to add a controller for Department I am getting an error as

unable to retrieve metadata for 'DMS.Models.Department' The navigation property 'L_ID' is not declared property on type Department.Verify that it has not been explicitly excluded from the model and that it is a  valid navigation property.
tereško
  • 58,060
  • 25
  • 98
  • 150
Ankur
  • 35
  • 1
  • 11
  • Try this link, this might help even more for you: http://stackoverflow.com/a/14354851/1983024 – Jon P Jan 17 '13 at 01:56

1 Answers1

0

[ForeignKey("LoactionID")]

public int L_ID { get; set; }

public Virtual Location Location {get;set;}

Make these changes in Department model and try once again. I hope this will solve your issue.

Sandip
  • 112
  • 8