Entity Framework Core One-to-One Relationship => Introducing FOREIGN KEY constraint
public class TaskEmployee
{
public int TaskEmployeeId { get; set; }
public string Title { get; set; }
public Shop Shop { get; set; }
public Employee Employee { get; set; }
}
public class TaskEmployee
{
public int TaskEmployeeId { get; set; }
public string Title { get; set; }
public Shop Shop { get; set; }
public Employee Employee { get; set; }
}
public class Shop
{
[Required]
public int ShopId { get; set; }
public string Name { get; set; }
public Address? Address { get; set; }
public int TaskEmployeeeId { get; set; }
public TaskEmployee TaskEmployee { get; set; }
}
public class Shop
{
[Required]
public int ShopId { get; set; }
public string Name { get; set; }
public Address? Address { get; set; }
public int TaskEmployeeeId { get; set; }
public TaskEmployee TaskEmployee { get; set; }
}
public class Employee
{
[Required]
public int EmployeeId { get; set; }
public string FirstName { get; set; }
public EmployeePosition EmployeePosition { get; set; }
[ForeignKey("ShopId")]
public int ShopId { get; set; }
public Shop Shop { get; set; }
public int TaskEmployeeeId { get; set; }
public TaskEmployee TaskEmployee { get; set; }
}
public class Employee
{
[Required]
public int EmployeeId { get; set; }
public string FirstName { get; set; }
public EmployeePosition EmployeePosition { get; set; }
[ForeignKey("ShopId")]
public int ShopId { get; set; }
public Shop Shop { get; set; }
public int TaskEmployeeeId { get; set; }
public TaskEmployee TaskEmployee { get; set; }
}
public modelBuilder.Entity<TaskEmployee>()
.HasOne(h => h.Employee)
.WithOne(h => h.TaskEmployee)
.HasForeignKey<Employee>(h => h.EmployeeId);
modelBuilder.Entity<TaskEmployee>()
.HasOne(h => h.Shop)
.WithOne(h => h.TaskEmployee)
.HasForeignKey<Shop>(h => h.ShopId);
public modelBuilder.Entity<TaskEmployee>()
.HasOne(h => h.Employee)
.WithOne(h => h.TaskEmployee)
.HasForeignKey<Employee>(h => h.EmployeeId);
modelBuilder.Entity<TaskEmployee>()
.HasOne(h => h.Shop)
.WithOne(h => h.TaskEmployee)
.HasForeignKey<Shop>(h => h.ShopId);
and error during update database: Introducing FOREIGN KEY constraint 'FK_TaskEmployees_Shops_TaskEmployeeId' on table 'TaskEmployees' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint or index. See previous errors.
5 Replies
public class Employee
{
[Required]
public int EmployeeId { get; set; }
public string FirstName { get; set; }
public EmployeePosition EmployeePosition { get; set; }
[ForeignKey("ShopId")]
public int ShopId { get; set; }
public Shop Shop { get; set; }
public int TaskEmployeeeId { get; set; }
public TaskEmployee TaskEmployee { get; set; }
}
public class Employee
{
[Required]
public int EmployeeId { get; set; }
public string FirstName { get; set; }
public EmployeePosition EmployeePosition { get; set; }
[ForeignKey("ShopId")]
public int ShopId { get; set; }
public Shop Shop { get; set; }
public int TaskEmployeeeId { get; set; }
public TaskEmployee TaskEmployee { get; set; }
}
relations one-to-many Shop with many Employee modelBuilder.Entity<Employee>()
.HasOne(h => h.Shop)
.WithMany(h => h.Employees)
.HasForeignKey(h => h.ShopId)
.IsRequired();
I remeber that I had problem also with this
any suggestion ? 😦
what do you actually want as the end goal?
a shop has many employees
an employee has one shop
and the employee has one task?
I want TaskEmployee has one Shop and one Employe
public class Shop
{
public int ShopId { get; set; }
public string Name { get; set; }
public Address? Address { get; set; }
// Navigation property for one-to-many relationship
public List<Employee> Employees { get; set; }
}
public class Employee
{
public int EmployeeId { get; set; }
public string FirstName { get; set; }
public EmployeePosition EmployeePosition { get; set; }
// Foreign key property for the many-to-one relationship with Shop
public int ShopId { get; set; }
// Navigation property for the many-to-one relationship with Shop
public Shop Shop { get; set; }
// Navigation property for the one-to-one relationship with TaskEmployee
public TaskEmployee TaskEmployee { get; set; }
}
public class TaskEmployee
{
public int TaskEmployeeId { get; set; }
public string Title { get; set; }
// Foreign key property for the one-to-one relationship with Employee
public int EmployeeId { get; set; }
// Foreign key property for the one-to-one relationship with Shop
public int ShopId { get; set; }
// Navigation properties
public Employee Employee { get; set; }
public Shop Shop { get; set; }
}
// Your DbContext class
modelBuilder.Entity<Employee>()
.HasOne(e => e.Shop)
.WithMany(s => s.Employees)
.HasForeignKey(e => e.ShopId);
modelBuilder.Entity<TaskEmployee>()
.HasOne(te => te.Employee)
.WithOne(e => e.TaskEmployee)
.HasForeignKey<TaskEmployee>(te => te.EmployeeId);
modelBuilder.Entity<TaskEmployee>()
.HasOne(te => te.Shop)
.WithOne(s => s.TaskEmployee)
.HasForeignKey<TaskEmployee>(te => te.ShopId);
public class Shop
{
public int ShopId { get; set; }
public string Name { get; set; }
public Address? Address { get; set; }
// Navigation property for one-to-many relationship
public List<Employee> Employees { get; set; }
}
public class Employee
{
public int EmployeeId { get; set; }
public string FirstName { get; set; }
public EmployeePosition EmployeePosition { get; set; }
// Foreign key property for the many-to-one relationship with Shop
public int ShopId { get; set; }
// Navigation property for the many-to-one relationship with Shop
public Shop Shop { get; set; }
// Navigation property for the one-to-one relationship with TaskEmployee
public TaskEmployee TaskEmployee { get; set; }
}
public class TaskEmployee
{
public int TaskEmployeeId { get; set; }
public string Title { get; set; }
// Foreign key property for the one-to-one relationship with Employee
public int EmployeeId { get; set; }
// Foreign key property for the one-to-one relationship with Shop
public int ShopId { get; set; }
// Navigation properties
public Employee Employee { get; set; }
public Shop Shop { get; set; }
}
// Your DbContext class
modelBuilder.Entity<Employee>()
.HasOne(e => e.Shop)
.WithMany(s => s.Employees)
.HasForeignKey(e => e.ShopId);
modelBuilder.Entity<TaskEmployee>()
.HasOne(te => te.Employee)
.WithOne(e => e.TaskEmployee)
.HasForeignKey<TaskEmployee>(te => te.EmployeeId);
modelBuilder.Entity<TaskEmployee>()
.HasOne(te => te.Shop)
.WithOne(s => s.TaskEmployee)
.HasForeignKey<TaskEmployee>(te => te.ShopId);
public class Shop
{
public int ShopId { get; set; }
public string Name { get; set; }
public Address? Address { get; set; }
// Navigation property for one-to-many relationship
public List<Employee> Employees { get; set; }
}
public class Employee
{
public int EmployeeId { get; set; }
public string FirstName { get; set; }
public EmployeePosition EmployeePosition { get; set; }
// Foreign key property
public int ShopId { get; set; }
// Navigation property for the many-to-one relationship with Shop
public Shop Shop { get; set; }
// Navigation property for the one-to-one relationship with Task
public TaskEmployee TaskEmployee { get; set; }
}
public class TaskEmployee
{
public int TaskEmployeeId { get; set; }
public string Title { get; set; }
// Foreign key property
public int EmployeeId { get; set; }
// Navigation property for the one-to-one relationship with Employee
public Employee Employee { get; set; }
}
modelBuilder.Entity<Employee>()
.HasOne(e => e.Shop)
.WithMany(s => s.Employees)
.HasForeignKey(e => e.ShopId);
modelBuilder.Entity<TaskEmployee>()
.HasOne(te => te.Employee)
.WithOne(e => e.TaskEmployee)
.HasForeignKey<TaskEmployee>(te => te.EmployeeId);
public class Shop
{
public int ShopId { get; set; }
public string Name { get; set; }
public Address? Address { get; set; }
// Navigation property for one-to-many relationship
public List<Employee> Employees { get; set; }
}
public class Employee
{
public int EmployeeId { get; set; }
public string FirstName { get; set; }
public EmployeePosition EmployeePosition { get; set; }
// Foreign key property
public int ShopId { get; set; }
// Navigation property for the many-to-one relationship with Shop
public Shop Shop { get; set; }
// Navigation property for the one-to-one relationship with Task
public TaskEmployee TaskEmployee { get; set; }
}
public class TaskEmployee
{
public int TaskEmployeeId { get; set; }
public string Title { get; set; }
// Foreign key property
public int EmployeeId { get; set; }
// Navigation property for the one-to-one relationship with Employee
public Employee Employee { get; set; }
}
modelBuilder.Entity<Employee>()
.HasOne(e => e.Shop)
.WithMany(s => s.Employees)
.HasForeignKey(e => e.ShopId);
modelBuilder.Entity<TaskEmployee>()
.HasOne(te => te.Employee)
.WithOne(e => e.TaskEmployee)
.HasForeignKey<TaskEmployee>(te => te.EmployeeId);