A referential integrity constraint violation occurred.

Nov 4, 2014 at 10:32 AM
Edited Nov 4, 2014 at 10:53 AM
I am receiving an error adding 2 related entities to the database. Can anyone spot where I am going wrong? I assume I am missing a relationship somewhere.

The error is "A referential integrity constraint violation occurred: The property value(s) of 'EntityA.ID' on one end of a relationship do not match the property value(s) of 'EntityB.EntityAID' on the other end."

This is what I've got...
   public partial class MyContext : DataContext
    {
        static MyContext()
        {
            Database.SetInitializer<MyContext>(null);
        }
        public MyContext()
            : base("Name=MyContext")
        {
        }
        public DbSet<EntityA> EntityA{ get; set; }
        public DbSet<EntityB> EntityB{ get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new EntityAMap());
            modelBuilder.Configurations.Add(new EntityBMap());
        }
    }

        public partial class EntityA : Entity
    {        
        public EntityA()
        {
            this.EntityBs = new List<EntityB>();
        }

        public int ID { get; set; }
        public string Name { get; set; }

        public virtual ICollection<EntityB> EntityBs { get; set; }
    }

    public partial class EntityB : Entity
    {
        public EntityB()
        {
        }

        public int ID { get; set; }
        public string Name { get; set; }
        public int EntityAID { get; set; }

        public virtual EntityA EntityA { get; set; }
    }

    public class EntityAMap : EntityTypeConfiguration<EntityA>
    {
        public EntityAMap()
        {
            // Primary Key
            this.HasKey(t => t.ID);

            // Table & Column Mappings
            this.ToTable("EntityA");
            this.Property(t => t.ID).HasColumnName("ID")
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            this.Property(t => t.Name).HasColumnName("Name");

        }
    }

    public class EntityBMap : EntityTypeConfiguration<EntityB>
    {
        public EntityBMap()
        {
            // Primary Key
            this.HasKey(t => t.ID);

            // Table & Column Mappings
            this.ToTable("EntityB");
            this.Property(t => t.ID).HasColumnName("ID")
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            this.Property(t => t.Name).HasColumnName("Name");
            this.Property(t => t.EntityAID).HasColumnName("EntityAID");

            // Relationships
            this.HasRequired(t => t.EntityA)
                .WithMany(t => t.EntityBs)
                .HasForeignKey(d => d.EntityAID);
        }
    }


    public void CreateEntity()
    {
        var entityA = new EntityA
        {
            Name = "EntityA"
        };

        entityA.EntityBs.Add(
            new EntityB{
            Name = "EntityB"
        });

        _entityAService.Insert(entityA);
                
        _unitOfWorkAsync.SaveChanges();
    }
Coordinator
Dec 20, 2014 at 1:48 AM
  1. All Entities must implement IObjectState, and make sure you set the ObjectState.
  2. Use InsertOrUpdateGraph since you are passing in an object graph.
Marked as answer by lelong37 on 12/19/2014 at 5:48 PM