Migrations in C# are quite well facilitated by the EntityFramework. In general, when an application with databse is created, initially the database model is taken from the model classes. When these models classes change, then they get out of sync with the database. Thus, each time the model is changed, the database should be “informed” as well, so it can adapt to the change. These “informing-s” of the model towards the database are called “migration”. In Visual Studio, to work with migrations, one may use the Package Manager Console (PMC) and write the migrations there.
These are some commands, which could be used in the C# PMC for migrations:
1 2 3 4 5 6 7 |
Drop-Database Add-Migration InitialCreate Update-Database Add-Migration Kursove Update-Database Remove-Migration Kursove Update-Database InitialCreate |
When the first “Add-Migration” command is executed, VisualStudio EF creates a folder Migrations, in which information about the migrations are kept. When the database is updated, a corresponding table for the migrations is created as well. This is the migration code, generated by a single name change of a table in a database:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
// <auto-generated /> using System; using ContosoUniversity.Data; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.EntityFrameworkCore.Migrations; using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace ContosoUniversity.Migrations { [DbContext(typeof(SchoolContext))] [Migration("20190827152603_Kursove")] partial class Kursove { protected override void BuildTargetModel(ModelBuilder modelBuilder) { #pragma warning disable 612, 618 modelBuilder .HasAnnotation("ProductVersion", "2.1.11-servicing-32099") .HasAnnotation("Relational:MaxIdentifierLength", 128) .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); modelBuilder.Entity("ContosoUniversity.Models.Course", b => { b.Property<int>("CourseId"); b.Property<int>("Credits"); b.Property<string>("Title"); b.HasKey("CourseId"); b.ToTable("Kursove"); }); modelBuilder.Entity("ContosoUniversity.Models.Enrollment", b => { b.Property<int>("EnrollmentId") .ValueGeneratedOnAdd() .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); b.Property<int>("CourseId"); b.Property<int?>("Grade"); b.Property<int>("StudentId"); b.HasKey("EnrollmentId"); b.HasIndex("CourseId"); b.HasIndex("StudentId"); b.ToTable("Inscriptiones"); }); modelBuilder.Entity("ContosoUniversity.Models.Student", b => { b.Property<int>("Id") .ValueGeneratedOnAdd() .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); b.Property<DateTime>("EnrollmentDate"); b.Property<string>("FirstMidName"); b.Property<string>("LastName"); b.HasKey("Id"); b.ToTable("Estudiantes"); }); modelBuilder.Entity("ContosoUniversity.Models.Enrollment", b => { b.HasOne("ContosoUniversity.Models.Course", "Course") .WithMany("Enrollments") .HasForeignKey("CourseId") .OnDelete(DeleteBehavior.Cascade); b.HasOne("ContosoUniversity.Models.Student", "Student") .WithMany("Enrollments") .HasForeignKey("StudentId") .OnDelete(DeleteBehavior.Cascade); }); #pragma warning restore 612, 618 } } } |
The rest of the project code is available in GitHub – https://github.com/Vitosh/ASP/tree/master/EFCoreAsp.NetMvcWebApp/ContosoUniversity004
Enjoy it!