0

I am just trying to enable migration in entity framework code first approach. I am working on AirlineManagementSystem.

I have created a class Plane.

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

namespace AirlineticketSystem.Models
{
    public class Plane
    {
        [Key]
        public int Plane_id { get; set; }
        public string Plane_Name { get; set; }
        public string Plane_No { get; set; }
        public string Plane_BClass { get; set; }
        public string Plane_EClass { get; set; }

        public List<User> Users { get; set; }
    }
}

Now I have created a class User

using System.ComponentModel.DataAnnotations;

namespace AirlineticketSystem.Models
{
  public class User { 

    public int userid { get; set; }
    public string user_name { get; set; }
    public string user_fname { get; set; }
    public string usercnic { get; set; }
    public string user_passport { get; set; }
    public string user_bloodGp { get; set; }
    public string user_nationality { get; set; }
    public string usertype { get; set; }
    public string status { get; set; }
    public int mobilenumber { get; set; }
    public string gender { get; set; }

   public int Plane_id { get; set; }
   public Plane Plane { get; set; }
    }

}

So One Plane can have many Users.

Please let me know how can I enable migration in the Package Manage Console??

  • Run first `Install-Package EntityFramework` to install Entity Framework, then `Enable-Migrations` on the Package Manager Console – Javier Gonzalez Feb 06 '18 at 19:14
  • Where you stuck? https://msdn.microsoft.com/en-us/library/jj591621(v=vs.113).aspx – Steve Greene Feb 06 '18 at 19:54
  • I would recomment marking the `Users` and `Plane` as `virtual` https://msdn.microsoft.com/en-us/library/jj679962(v=vs.113).aspx – nurdyguy Feb 06 '18 at 21:50
  • Virtual not required unless you want [lazy loading](https://stackoverflow.com/questions/5597760/what-effects-can-the-virtual-keyword-have-in-entity-framework-4-1-poco-code-fi). – Steve Greene Feb 06 '18 at 23:15
  • Thank you so much @SteveGreene . It really helped. Now I can see the tables in the local db. Just can't see in the SQL Server. trying to find out . –  Feb 08 '18 at 18:55
  • Do you mean you want 2 databases - 1 localdb, 1 sql server? That would just be a connection string change. – Steve Greene Feb 09 '18 at 14:29
  • No I need a single database. But I can see the table in the local db SQL Server Object Explorer (dbo.Blogs dbo.Posts dbo.Users) as I have implemented through your mentioned link. How can I see these tables in the Microsoft SQL Server Management Studio? –  Feb 09 '18 at 18:18

1 Answers1

0

You can follow these instruction as below. It is clearly understandable https://msdn.microsoft.com/en-us/library/jj193542(v=vs.113).aspx

You can add a virtual key for reference class(public virtual List<User> Users { get; set; }). If you ask why, you can read this What effect(s) can the virtual keyword have in Entity Framework 4.1 POCO Code First?

Sinan Barut
  • 510
  • 1
  • 6
  • 14