As per the image above, I have a requirement where, Table C needs to refer both Table A and Table B.
Note: RefId
in Table_C is a reference key for both Tables A and B.
Please, refer the code snippets,
Table_A Class
public partial class Table_A
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Table_C> Table_C { get; set; }
}
Table_B Class
public partial class Table_B
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Table_C> Table_C { get; set; }
}
Table_C Class
public partial class Table_C
{
public int Id { get; set; }
public int RefId { get; set; }
public Type Type {get; set; }
public virtual Table_A Table_A { get; set; }
public virtual Table_B Table_B { get; set; }
}
Fluent API
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Table_1>()
.Property(e => e.Name)
.IsFixedLength();
modelBuilder.Entity<Table_1>()
.HasMany(e => e.Table_3)
.WithRequired(e => e.Table_1)
.HasForeignKey(e => e.RefId)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Table_2>()
.Property(e => e.Name)
.IsFixedLength();
modelBuilder.Entity<Table_2>()
.HasMany(e => e.Table_3)
.WithRequired(e => e.Table_2)
.HasForeignKey(e => e.RefId)
.WillCascadeOnDelete(false);
}
However, the below error occurs when trying to achieve this requirement using the code shown above.
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.TableC.TableA_Id". The conflict occurred in database "TestDB", table "dbo.TableA", column 'Id'. The statement has been terminated
How can this be implemented using Entity Framework 6 (SQL Server 2014, .NET framework 4.6.1)?