// Create class
User.cs
public class User
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string FirstName { get; set; }
public virtual List<Job> jobs {get; set; } // 1 to many
}
Job.cs
public class Job
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string Name { get; set; }
public int UserId { get; set; }
[ForeignKey("UserId")]
public virtual User User { get; set; }
}
DBContext.cs
public class DBContext: DbContext
{
public DBContext(DbContextOptions<MySQLDBContext> options) : base(options) { }
public DbSet<User> User { get; set; }
public DbSet<Job> Job { get; set; }
}
// Principal (parent)
public class Blog
{
public int Id { get; set; }
public ICollection<Post> Posts { get; } = new List<Post>(); // Collection navigation containing dependents
}
// Dependent (child)
public class Post
{
public int Id { get; set; }
public int BlogId { get; set; } // Required foreign key property
public Blog Blog { get; set; } = null!; // Required reference navigation to principal
}
Create a database
Run and Compilation, check tables have been generated.
// Add-Migration
// VS => NuGet Package Manager > Package Manager Console
enable-migrations // creates a Migrations folder and Configuration.cs file
add-migration InitialCreate // change database
update-database // update to database server
name: AddUrltoTablePicture (for example)
<<Change anything in up() function which generated>>
update-database
// Error: cause cycles or multiple cascade paths
change:
onDelete: ReferentialAction.Cascade);
to:
onDelete: ReferentialAction.Restrict);