5
Answers

Code First Issues

This thing is really huge for me and your patience to read and understand this whole code will be highly appreciable.

I am using code first approach in this project and when I run this I got the error shown below See the code firstly.

public class Jobs
{
    [Key]
    public int JobId { get; set; }
     [Display(Name = "Title")]
    [Required(ErrorMessage = "*")]
    public string Title { get; set; }
   [Display(Name = "Details:")]
    [Required(ErrorMessage = "*")]
    [AllowHtml]
    public string Description { get; set; }
    [Display(Name = "Job Type")]
    [MaxLength(10, ErrorMessage = "Supplied value is not valid for job type ")]
    [Required(ErrorMessage = "*")]
    public string JobType { get; set; }
    [Display(Name = "City")]
    public int CityId { get; set; }
    public virtual City City { get; set; }
    public int EmpId { get; set; }
    public virtual Employer Employer { get; set; }
    public int CmpId { get; set; }
    public virtual Company Company { get; set; }
    [Display(Name = "Academics")]
    public int EduId { get; set; }
    public virtual Education Education { get; set; }
    //put just for test
    [NotMapped]
    public int NumOfApplications { get; set; }
}

public class Employer
{
    [Key]
    public int EmpId { get; set; }
    [MaxLength(30)]
    public string FirstName { get; set; }
    [MaxLength(30)]
    public string LastName { get; set; }
    [MaxLength(50)]
    public string Occupation { get; set; }
    //put just for test
    [NotMapped]
    public int NumOfApplications { get; set; }
    public virtual ICollection<Company> Companies { get; set; }
    public virtual ICollection<Application> Applications { get; set; }
    public virtual ICollection<Jobs> PostedJobs { get; set; }
    public string Id { get; set; }
    //public virtual ApplicationUser User { get; set; } //navigation property
}

public class Company
{
    [Key]
    public int CmpId { get; set; }
    public string CompanyName { get; set; }
    [MaxLength(30)]
    public string Designation { get; set; }
    [MaxLength(30)]
    public string CompleteAddress { get; set; }
    [MaxLength(30)]
    public string Phone { get; set; }
    [MaxLength(50)]
    public string Sector { get; set; }
    public DateTime? EstablisedIn { get; set; }
    public string Logo { get; set; }
    public string About { get; set; }
    public int NumberOfEmployees { get; set; }
    public bool Status { get; set; }
    public int EmpId { get; set; }
    public virtual Employer Employer { get; set; }
}

public class Application
{
    [Key]
    public int AppId { get; set; }
    public DateTime ApplyDate { get; set; }
    public int EmpId { get; set; }
    public virtual Employer Employer { get; set; }
    public int JobId { get; set; }
    public virtual Jobs Job { get; set; }
    public int CanId { get; set; }
    public virtual Candidate Candidate { get; set; }
}

public class Education
{
    [Key]
    public int EduId { get; set; }
    [StringLength(50)]
    public string EduName { get; set; }
}

public class City
{
    [Key]
    public int CityId { get; set; }
    [MaxLength(30)]
    public string CityName { get; set; 
}   

Introducing FOREIGN KEY constraint 'FK_dbo.Jobs_dbo.Companies_CmpId' on table 'Jobs' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Could not create constraint. See previous errors.

So I configure flent api which is really below when I again run the code in debugging

Catch block shows error Invalid column name Employer_EmpId1, and then application crash

 I have checked everything in my db but I cant find this (Employer_EmpId1) foreign key relation. I am sure that there is an issue with fluent Api configuration can any one please help me out or give another solution to solve the issue

In debugging I got  something  which I don’t understand just pasting it

Sql = "SELECT \r\n    [UnionAll1].[EmpId] AS [C1], \r\n    [UnionAll1].[EmpId1] AS [C2], \r\n    [UnionAll1].[FirstName] AS [C3], \r\n    [UnionAll1].[LastName] AS [C4], \r\n    [UnionAll1].[Occupation] AS [C5], \r\n    [UnionAll1].[Email] AS [C6], \r\n    [Union...

Here  I don’t understand what is  EmpId1 , where it comes from

Fluent Api Code

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Jobs>().
         HasRequired<Company>(k => k.Company).
         WithMany().
         WillCascadeOnDelete(false);
    modelBuilder.Entity<Jobs>().
         HasRequired<Employer>(k => k.Employer).
         WithMany().
         WillCascadeOnDelete(false);
}

My Controller.cs Code

public ActionResult Account(Employer e)
{
    EmployerViewModel emp = new EmployerViewModel();
    if (e.Id != null)
    {
        try {
            var data = from em in db.Employers.Include("Companies").Include("Jobs")
                       where em.Id == e.Id
                       select new
                       {
                           a = em.Companies,
                           b = em,
                           c = em.PostedJobs
                       };
            foreach (var item in data)
            {
                emp.Emp.Companies = item.a;
                emp.Emp = item.b;
                emp.Jobs = item.c;
            }
        }
        catch (Exception ex)
        {
            ex.ToString();
        }
    }
    return View(emp);
}

 

Answers (5)