Unable to save new Entities with One-To-Many relation in Entity Framework

c# entity-framework entity-framework-6

Question

When trying to save two new entities with a One-To-Many relation, I am getting the following error:

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.

I am using EF v6.1.3

Here is an example of the code:

var entity = _dbContext.Set<EntityA>().Create();

entity.Identifier = 1234;
entity.Title = "Test Title";
entity.Description = "Test Description";

_dbContext.Set<EntityA>().Add(entity);

var relatedEntity = _dbContext.Set<EntityB>().Create();

relatedEntity.Identifier = "123-1234";
relatedEntity.Title = "Test Title";
relatedEntity.Statement = "Sample Statement";
relatedEntity.Entity = entity;

_dbContext.Set<EntityB>().Add(relatedEntity);
_dbContext.SaveChanges();

The only way I can get this to not give the error is to call _dbContext.SaveChanges(); before creating the related entity.

Any help would be greatly appreciated.

EDIT

Here is the SQL for the tables:

CREATE TABLE [dbo].[EntityA]
(
    [Id] INT NOT NULL IDENTITY(1,1) PRIMARY KEY,
    [Identifier] NVARCHAR (100) NULL,
    [Title] NVARCHAR(100) NULL,
    [Description] NVARCHAR (2000) NULL
)

CREATE TABLE [dbo].[EntityB]
(
    [Id] INT NOT NULL PRIMARY KEY IDENTITY,
    [Identifier] NVARCHAR (100) NULL,
    [Title] NVARCHAR(100) NULL,
    [Statement] NVARCHAR (2000) NOT NULL,
    [EntityAId] INT NOT NULL,
    CONSTRAINT [FK_EntityB_EntityA] FOREIGN KEY ([EntityAId]) REFERENCES [EntityA]([Id])
)

Here is the OnModelCreating configuration:

modelBuilder.Entity<EntityA>()
    .HasMany(e => e.RelatedEntities)
    .WithRequired(e => e.EntityA)
    .HasForeignKey(e => e.EntityAId);

Here are the Entity Classes:

[Table("EntityA")]
public class EntityA
{
    public int Id { get; set; }

    public string Identifier { get; set; }

    public string Title { get; set; }

    public string Description { get; set; }

    public virtual IList<EntityB> RelatedEntities { get; set; }
}

[Table("EntityB")]
public class EntityB
{
    public int Id { get; set; }

    public int EntityAId { get; set; }

    public string Identifier { get; set; }

    public string Title { get; set; }

    public string Statement { get; set; }
}
1
1
12/9/2016 6:59:10 PM

Popular Answer

The moment when you create the Entity2, Entity(1) is not yet in the database and Entity2 needs to have 1 available to create itself. You do right thing to call SaveChanges() to create Entity(1) first.

0
12/9/2016 6:33:11 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow