1

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
China Syndrome
  • 953
  • 12
  • 24
  • 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. – Robert Harvey Jan 25 '21 at 15:57
  • yes I have googled and not found any solution that work this is a standard design and EF should allow me to delete the object – China Syndrome Jan 25 '21 at 16:32
  • Well, you need to read the error message and work through its implications. Google will only tell you what you already know: you're trying to remove a record without nulling its non-nullable foreign key in the host record, defining a new relationship or assigning a new null value. To correct the problem, you have to determine which condition is causing it and correct that condition. – Robert Harvey Jan 25 '21 at 16:34
  • Then maybe you can clarify why the suggestions in the error message don't apply to your particular case. The community at Stack Overflow expects you to show your research. Tell us what you've tried to fix the problem. This helps us narrow down the specific issue, and avoids us reiterating solutions you've already tried (like the ones in the error message). – Robert Harvey Jan 25 '21 at 19:18
  • Duplicate https://stackoverflow.com/q/5538974 – Robert Harvey Jan 25 '21 at 19:25

0 Answers0