I have 3 tables Groups, Organizations and Rel_Group_Organization
I am trying to delete a record or all records
BO.RelGroupOrganization.Clear();
BO.RelGroupOrganization.Remove(record to delete);
_context.SaveChanges();
when saveChanged I get the following System.InvalidOperationException: 'The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.'
Here is my configuration
public RelGroupOrganizationMap()
{
ToTable("REL_GROUP_ORGANIZATION");
Property(e => e.Id).HasColumnName("ID");
Property(e => e.GroupId).HasColumnName("GROUP_ID");
Property(e => e.OrganizationId).HasColumnName("ORGANIZATION_ID") ;
HasRequired(s => s.Organization).WithMany().HasForeignKey(key => key.OrganizationId);
HasRequired(s => s.RapidGroup).WithMany().HasForeignKey(key => key.GroupId);
}
here is my class
public partial class RelGroupOrganization
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public int GroupId { get; set; }
public int OrganizationId { get; set; }
public virtual LuOrganization Organization { get; set; }
public virtual RapidGroup RapidGroup { get; set; }
}
finally here is the sql for the table
CREATE TABLE [dbo].[REL_GROUP_ORGANIZATION](
[ID] [int] NOT NULL,
[GROUP_ID] [int] NOT NULL,
[ORGANIZATION_ID] [int] NOT NULL,
CONSTRAINT [PK_GROUP_ORGANIZATION] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 80) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[REL_GROUP_ORGANIZATION] ADD CONSTRAINT [DF_REL_GROUP_ORGANIZATION_SEQ] DEFAULT (NEXT VALUE FOR [dbo].[REL_GROUP_ORGANIZATION_SEQ]) FOR [ID]
GO
ALTER TABLE [dbo].[REL_GROUP_ORGANIZATION] WITH CHECK ADD CONSTRAINT [FK_REL_GRP_RGANIZATIN_GRP_ID] FOREIGN KEY([GROUP_ID])
REFERENCES [dbo].[RAPID_GROUP] ([ID])
GO
ALTER TABLE [dbo].[REL_GROUP_ORGANIZATION] CHECK CONSTRAINT [FK_REL_GRP_RGANIZATIN_GRP_ID]
GO
ALTER TABLE [dbo].[REL_GROUP_ORGANIZATION] WITH CHECK ADD CONSTRAINT [FK_REL_GRP_RGANZATN_RGANZATN_D] FOREIGN KEY([ORGANIZATION_ID])
REFERENCES [dbo].[LU_ORGANIZATION] ([ID])
GO