dotnet-starter-kit icon indicating copy to clipboard operation
dotnet-starter-kit copied to clipboard

Foreign key error

Open aydinbskr opened this issue 2 years ago • 1 comments

At some tables, the system not add inner join

aydinbskr avatar Sep 23 '22 17:09 aydinbskr

Same issue:

I have class as belowing.

  • When I create a employee with DepartmentId & SubDepartmentId != null => Sucessfully !
  • When I create a employee with DepartmentId != null & SubDepartmentId = null => Failed with error:

(0x80131904): The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Employees_SubDepartments_SubDepartmentId". The conflict occurred in database "rootTenantDb", table "SubDepartments", column 'Id'.

How to fix or work around it ???

public class Employee : AuditableEntity, IAggregateRoot { public string FirstName { get; private set; } public string? LastName { get; private set; ..... public DefaultIdType DepartmentId { get; private set; } public virtual Department Department { get; private set; } = default!;

public DefaultIdType? SubDepartmentId { get; private set; } public virtual SubDepartment SubDepartment { get; private set; } = default!; }

public class SubDepartment : AuditableEntity, IAggregateRoot { public string Name { get; private set; } = default!; public string Description { get; private set; } = string.Empty;

public DefaultIdType DepartmentId { get; private set; } public virtual Department Department { get; private set; } = default!; }

public class Department : AuditableEntity, IAggregateRoot {

public string Name { get; private set; } = default!; public string Description { get; private set; } = string.Empty; }

Mike6x avatar Dec 23 '22 03:12 Mike6x