According to the current information, I think CustomerAddress and CustomerPhoneNumber should both have a foreign key to Customer, right?
I defined two tables for testing: Person and Book
In the code, their corresponding classes are like this:
[Table("Book")]
public partial class Book
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int ID { get; set; }
[StringLength(10)]
public string Name { get; set; }
public decimal? Price { get; set; }
public int? OwnerID { get; set; }
public virtual Person Person { get; set; }
}
[Table("Person")]
public partial class Person
{
public Person()
{
Books = new HashSet<Book>();
}
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int ID { get; set; }
[StringLength(10)]
public string Name { get; set; }
public virtual ICollection<Book> Books { get; set; }
}
When I insert data, I only need a Person repository to insert data into two tables.
Person person = new Person() { ID = 5, Name = "test2" };
books.Add(new Book() { ID = 6, Name = "book6", OwnerID = 3,Price = 55, Person = person });
person.Books = books;
UnitOfWork unitOfWork = new UnitOfWork();
unitOfWork.PersonRepository.Insert(person);
unitOfWork.Save();
The UnitOfWork class and GenericRepository class come from the documentation you provided.
The document I refer to: Relationships
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.