Entity Framework – Code First migrations

Date: 2017-03-20
# Open [Package Manager Console] in Visual Studio
# Select the main Entity Framework Project at [Default project:]
# Execute the lines below on de [PM>] lines
Add-Migration initial
Update-Database -script
Add-Migration "Change 2"
Update-Database -script


using System.Data.Entity;

namespace ApplicationDb.BusinessObjects
{
    public partial class ApplicationDbContext : DbContext
    {
        static ApplicationDbContext()
        {
            //Database.SetInitializer(new ApplicationDbDbInitializer());
            Database.SetInitializer(null);
        }

        public ApplicationDbContext() : base("ApplicationDbContext")
        {
            //this.Configuration.LazyLoadingEnabled = false;   
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.AddFromAssembly(typeof(ApplicationDbContext).Assembly);
            base.OnModelCreating(modelBuilder);
        }
    }
}


namespace BusinessObjects
{
    public partial class Absence : BaseBusinessObject
    {
        public int Id { get; set; }
        public string EmployeeId { get; set; }
        public System.DateTime DateFrom { get; set; }
        public System.DateTime DateTo { get; set; }
        public string Reason { get; set; }
        public int Status { get; set; }
        public System.DateTime DateChanged { get; set; }
        public System.DateTime DateCreated { get; set; }
        public string AbsentTypeCode { get; set; }
        public string FiatComment { get; set; }
        public virtual AbsentType AbsentType { get; set; }
        public virtual Employee Employee { get; set; }
    }
}

using System.Data.Entity.ModelConfiguration;
namespace BusinessObjects.Mapping
{
    public class AbsenceMap : EntityTypeConfiguration
    {
        public AbsenceMap()
        {
            // Primary Key
            this.HasKey(t => t.Id);

            // Properties
            this.Property(t => t.EmployeeId)
                .IsRequired()
                .HasMaxLength(50);

            this.Property(t => t.Reason)
                .HasMaxLength(50);

            this.Property(t => t.AbsentTypeCode)
                .IsRequired()
                .HasMaxLength(50);

            this.Property(t => t.FiatComment)
                .HasMaxLength(255);

            this.Property(t => t.RejectMessage)
                .HasMaxLength(1024);

            // Table & Column Mappings
            this.ToTable("Absence");
            this.Property(t => t.Id).HasColumnName("Id");
            this.Property(t => t.EmployeeId).HasColumnName("EmployeeId");
            this.Property(t => t.DateFrom).HasColumnName("DateFrom");
            this.Property(t => t.DateTo).HasColumnName("DateTo");
            this.Property(t => t.Reason).HasColumnName("Reason");
            this.Property(t => t.Status).HasColumnName("Status");
            this.Property(t => t.DateChanged).HasColumnName("DateChanged");
            this.Property(t => t.DateCreated).HasColumnName("DateCreated");
            this.Property(t => t.FiatComment).HasColumnName("FiatComment");                        

            // Relationships
            this.HasRequired(t => t.AbsentType)
                .WithMany(t => t.Absences)
                .HasForeignKey(d => d.AbsentTypeCode);
            this.HasRequired(t => t.Employee)
                .WithMany(t => t.Absences)
                .HasForeignKey(d => d.EmployeeId);

        }
    }
}


6790cookie-checkEntity Framework – Code First migrations