0

I have the following model in my application, but when I try to generate a view on the controller using it I get the error "Unable to retrieve metadata for Jop_Offers_Website.Models.JobRequest":

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

namespace Jop_Offers_Website.Models
{
    public class JobRequest 
    {
        public int Id { get; set; }
        public string Message { get; set; }
        public DateTime ApplyDate { get; set; }

        public int JobId { get; set; }
        public string UserId { get; set; }

        public virtual Jobs job { get; set; }
        public virtual ApplicationUser user { get; set; }
    }
}

Error dialog

When I use other models to add view, or if I comment out the job and user properties the view is generated successfully.

The Jobs Model:

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

namespace Jop_Offers_Website.Models
{
    public class Jobs
    {
        //id to be primary key of Jobs table
        public int Id { get; set; }

        //job title
        [Required]
        [Display (Name ="أسم الوظيفة")]
        public string JobTitle { get; set; }

        //job description
        [Required]
        [Display(Name ="وصف الوظيفة ")]
        public string JobDescription { get; set; }

        //jop image
        [Display(Name ="صورة الوظيفة ")]
        public string JobImage { get; set; }

        //id of categories to relate to Job category Type 1 to many relationship
        //[Required]
        public int CategoryId { get; set; }

        //object of category to detect job category 
        public virtual categories category { get; set; }

    }
}

The ApplicationUser model:

using System.Data.Entity;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;

namespace WebApplication3.Models
{
    // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit https://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
    public class ApplicationUser : IdentityUser

    {
        public string UserType { get; set; }
        public string Neabouring { get; set; }
        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }
    }

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("JobConnection", throwIfV1Schema: false)
        {
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }

        public System.Data.Entity.DbSet<Jop_Offers_Website.Models.categories> categories { get; set; }

        public System.Data.Entity.DbSet<Jop_Offers_Website.Models.Jobs> Jobs { get; set; }

        public System.Data.Entity.DbSet<Jop_Offers_Website.Models.RoleViewModel> RoleViewModels { get; set; }


        public System.Data.Entity.DbSet<Jop_Offers_Website.Models.JobRequest> JobRequests { get; set; }

    }
}

Any model used to add views but I can't add the view by using JobRequest Model and when I use JobRequest model without ApplicationDbContext the view is generated successfully .

Zhaph - Ben Duguid
  • 26,785
  • 5
  • 80
  • 117
M.Atef
  • 1
  • 2
  • Possible duplicate of [Add controller error unable to retrieve metadata](https://stackoverflow.com/questions/15445786/add-controller-error-unable-to-retrieve-metadata) – Sunil Jan 09 '18 at 13:05
  • 1
    First of all You clearly you need to learn more about MVC, which i suggest you start reading [this](https://www.asp.net/mvc/overview). **Second**, [Creating a View In ASP.NET MVC](https://learn.microsoft.com/en-us/aspnet/mvc/overview/older-versions/mvc-music-store/mvc-music-store-part-3) – Masoud Andalibi Jan 09 '18 at 13:06
  • Why Jobs and ApplicationUser need to be virtual in model ? – Arkadiusz Jan 09 '18 at 13:09
  • @Valkyrie that is not controller error because all models can be used to add view , and this model can be added without using DBContext – M.Atef Jan 09 '18 at 13:19
  • Please provide us a definition for Jobs and ApplicationUser class. – Arkadiusz Jan 09 '18 at 13:30
  • @Arkadiusz Done – M.Atef Jan 09 '18 at 13:41
  • @M.Atef Making the navigation properties virtual **enables** [lazy loading](https://stackoverflow.com/a/7738919/33051) – Zhaph - Ben Duguid Jan 10 '18 at 10:39
  • @Zhaph-BenDuguid Yes, I was wrong – M.Atef Jan 11 '18 at 06:03

0 Answers0