{"id":679,"date":"2017-03-20T11:27:54","date_gmt":"2017-03-20T10:27:54","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=679"},"modified":"2021-01-12T15:26:42","modified_gmt":"2021-01-12T14:26:42","slug":"entity-framework-code-first-migrations","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/entity-framework-code-first-migrations\/","title":{"rendered":"Entity Framework &#8211; Code First migrations"},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Open [Package Manager Console] in Visual Studio\n# Select the main Entity Framework Project at [Default project:]\n# Execute the lines below on de [PM>] lines\nAdd-Migration initial\nUpdate-Database -script\nAdd-Migration \"Change 2\"\nUpdate-Database -script\n\n\nusing System.Data.Entity;\n\nnamespace ApplicationDb.BusinessObjects\n{\n    public partial class ApplicationDbContext : DbContext\n    {\n        static ApplicationDbContext()\n        {\n            \/\/Database.SetInitializer(new ApplicationDbDbInitializer());\n            Database.SetInitializer(null);\n        }\n\n        public ApplicationDbContext() : base(\"ApplicationDbContext\")\n        {\n            \/\/this.Configuration.LazyLoadingEnabled = false;   \n        }\n\n        protected override void OnModelCreating(DbModelBuilder modelBuilder)\n        {\n            modelBuilder.Configurations.AddFromAssembly(typeof(ApplicationDbContext).Assembly);\n            base.OnModelCreating(modelBuilder);\n        }\n    }\n}\n\n\nnamespace BusinessObjects\n{\n    public partial class Absence : BaseBusinessObject\n    {\n        public int Id { get; set; }\n        public string EmployeeId { get; set; }\n        public System.DateTime DateFrom { get; set; }\n        public System.DateTime DateTo { get; set; }\n        public string Reason { get; set; }\n        public int Status { get; set; }\n        public System.DateTime DateChanged { get; set; }\n        public System.DateTime DateCreated { get; set; }\n        public string AbsentTypeCode { get; set; }\n        public string FiatComment { get; set; }\n        public virtual AbsentType AbsentType { get; set; }\n        public virtual Employee Employee { get; set; }\n    }\n}\n\nusing System.Data.Entity.ModelConfiguration;\nnamespace BusinessObjects.Mapping\n{\n    public class AbsenceMap : EntityTypeConfiguration\n    {\n        public AbsenceMap()\n        {\n            \/\/ Primary Key\n            this.HasKey(t => t.Id);\n\n            \/\/ Properties\n            this.Property(t => t.EmployeeId)\n                .IsRequired()\n                .HasMaxLength(50);\n\n            this.Property(t => t.Reason)\n                .HasMaxLength(50);\n\n            this.Property(t => t.AbsentTypeCode)\n                .IsRequired()\n                .HasMaxLength(50);\n\n            this.Property(t => t.FiatComment)\n                .HasMaxLength(255);\n\n            this.Property(t => t.RejectMessage)\n                .HasMaxLength(1024);\n\n            \/\/ Table &amp; Column Mappings\n            this.ToTable(\"Absence\");\n            this.Property(t => t.Id).HasColumnName(\"Id\");\n            this.Property(t => t.EmployeeId).HasColumnName(\"EmployeeId\");\n            this.Property(t => t.DateFrom).HasColumnName(\"DateFrom\");\n            this.Property(t => t.DateTo).HasColumnName(\"DateTo\");\n            this.Property(t => t.Reason).HasColumnName(\"Reason\");\n            this.Property(t => t.Status).HasColumnName(\"Status\");\n            this.Property(t => t.DateChanged).HasColumnName(\"DateChanged\");\n            this.Property(t => t.DateCreated).HasColumnName(\"DateCreated\");\n            this.Property(t => t.FiatComment).HasColumnName(\"FiatComment\");                        \n\n            \/\/ Relationships\n            this.HasRequired(t => t.AbsentType)\n                .WithMany(t => t.Absences)\n                .HasForeignKey(d => d.AbsentTypeCode);\n            this.HasRequired(t => t.Employee)\n                .WithMany(t => t.Absences)\n                .HasForeignKey(d => d.EmployeeId);\n\n        }\n    }\n}\n\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[6],"tags":[],"class_list":["post-679","post","type-post","status-publish","format-standard","hentry","category-dotnet"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/679","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=679"}],"version-history":[{"count":4,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/679\/revisions"}],"predecessor-version":[{"id":4455,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/679\/revisions\/4455"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=679"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=679"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=679"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}