locked
Entity Framework Code First - Able to migrate and update database but no table created RRS feed

  • Question

  • User521171331 posted

    Hi, before this, I am able to create table after I run

    "enable-migrations" 

    "add-migration abc"

    "update-database"

    After I add the foreign key, I can't see my new tables but I can run the above command with no error. Can someone tell what can possible gone wrong?

    These are my 3 classes for the model

    public class Employee
        {
            /// <summary>
            /// Gets or sets Name.
            /// </summary>
            public int Id { get; set; }
    
            [Required]
            [Display(Name = "Full Name")]
            public string Name { get; set; }
    
            [Required]
            [Display(Name = "Date of Birth")]
            public string DateOfBirth { get; set; }
    
            [Required]
            [Display(Name = "Employement Type")]
            public string EmploymentType { get; set; }        
    
            [Required]
            [Display(Name = "Basic Salary")]
            public int BasicSalary { get; set; }
    
            public virtual ICollection<Attendance> Attendances { get; set; }
        }
    public class Attendance
        {
            public int Id { get; set; }
    
            public int WorkHour { get; set; }
    
            public DateTime WorkDate { get; set; }
    
            public int EmployeeID { get; set; }
            public virtual Employee Employee { get; set; }
    
        }
     public class PayrollReport
        {
            public int Id { get; set; }
            
            [Required]
            [Display(Name = "Net Salary")]
            public int NetSalary { get; set; }
    
            public int EmployeeID { get; set; }
            public virtual Employee Employee { get; set; }
        }

    and my dbcontext code is:

     public class myDBContext : DbContext
        {        
            public myDBContext() : base("myDB")
            {
            }
    
            public DbSet<Employee> Employees { get; set; }
            public DbSet<Attendance> Attendances { get; set; }
            public DbSet<PayrollReport> PayrollReports { get; set; }
    
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
                
            }

    Saturday, December 2, 2017 5:46 PM

All replies

  • User-832373396 posted

    Hi ngaisteve1,

    Sir, It seems that your code looks good.

    After I add the foreign key, I can't see my new tables but I can run the above command with no error. Can someone tell what can possible gone wrong?

    Do you mean that the tables disappear? May I know what tables in the database until now? 

    At first, I suggest that you could refresh the database and check the tables. 

    and you could check the abc.cs migration file and view the code what it exactly does whether executed table deleting

    Good Luck to you :)

    With regards, Angelina Jolie

    Monday, December 4, 2017 6:18 AM
  • User753996341 posted

    Database Initialization

    Monday, December 4, 2017 7:50 AM