1. Create .netCore Web project. At this time, also lacks relationship with EF.
2. Then add classes Bolg. Post reference herein and https://docs.microsoft.com/en-us/ef/core/modeling/relational/fk-constraints
public class Blog { public int BlogId { get; set; } public string Url { get; set; } public List<Post> Posts { get; set; } } public class Post { public int PostId { get; set; } public string Title { get; set; } public string Content { get; set; } public int BlogId { get; set; } public Blog Blog { get; set; } }
3. The new controller Blogs. Use "" The operation of the EF controller. "
This time the relevant libraries will be installed from the EF Nuget.
This class also generates context
public class MyEFTestContext : DbContext { public MyEFTestContext (DbContextOptions<MyEFTestContext> options) : base(options) { } public DbSet<MyEFTest.Models.Blog> Blog { get; set; } }
4. Run add-migration. Folder generated migration. The following will migrate files and snap.cs file.
can be seen. The migration file.
modelBuilder.Entity("MyEFTest.Models.Post", b => { b.HasOne("MyEFTest.Models.Blog", "Blog") .WithMany("Posts") .HasForeignKey("BlogId") .OnDelete(DeleteBehavior.Cascade); });
This specifies the foreign key relationships