Hi all
I have been studying CRUD operations in web applications following tutorials (Blazor, MVC, etc), but none of them uses more than one table in the DB. So now I'm trying to test something like an agenda in a web application. So I have two tables (Models), they are:
1) People:
IdPerson
FullName
EMail
Address
2) Telephone
IdPhone
PhoneNumber
Description
IdPerson
where IdPerson from Telephone is a FK that points to IdPerson, that is PK from People.
My question is how to set this relation (FK_Telephone_People) in the OnModelCreating() method (which is in the class that inherits from DbContext). By now I have this:
Other question is: Do I have to add a List<Telephone> to the People Model, to be able to display data in the front end, wether it is Blazor, MVC, or wathever?
Note that a Person can have many PhoneNumbers, but they can only have one Person. (Is this called "one to many"?)
Note: I'm trying to create the DB from code, using a Migration and so (Is this called "code first"?).
I would strongly appreciate your valuable help, as usual.
Thanks
Pablo
(Buenos Aires - Argentina)
I have been studying CRUD operations in web applications following tutorials (Blazor, MVC, etc), but none of them uses more than one table in the DB. So now I'm trying to test something like an agenda in a web application. So I have two tables (Models), they are:
1) People:
IdPerson
FullName
Address
2) Telephone
IdPhone
PhoneNumber
Description
IdPerson
where IdPerson from Telephone is a FK that points to IdPerson, that is PK from People.
My question is how to set this relation (FK_Telephone_People) in the OnModelCreating() method (which is in the class that inherits from DbContext). By now I have this:
C#:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<People>(table =>
{
table.HasKey(col => col.IdPerson);
table.Property(col => col.IdPerson).UseIdentityColumn().ValueGeneratedOnAdd();
table.Property(col => col.FullName).HasMaxLength(50);
table.Property(col => col.EMail).HasMaxLength(40);
table.Property(col => col.Address).HasMaxLength(50);
});
modelBuilder.Entity<Telephone>(table =>
{
table.HasKey(col => col.IdPhone);
table.Property(col => col.IdPhone).UseIdentityColumn().ValueGeneratedOnAdd();
});
}
Other question is: Do I have to add a List<Telephone> to the People Model, to be able to display data in the front end, wether it is Blazor, MVC, or wathever?
Note that a Person can have many PhoneNumbers, but they can only have one Person. (Is this called "one to many"?)
Note: I'm trying to create the DB from code, using a Migration and so (Is this called "code first"?).
I would strongly appreciate your valuable help, as usual.
Thanks
Pablo
(Buenos Aires - Argentina)