Issue when perform delete cascade

Jan 27, 2015 at 12:49 PM
What is the correct way to perform 'delete cascade' using this framework? The detail issue shown below:
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 simple class for POC purpose:
public class Master
{
    public int Id { get; set; } 
    public virtual ICollection<Detail> Details { get; set; }
}

public class Detail
{
    public int Id { get; set; } 
    public int MasterId { get; set; }   
    public virtual Master Master { get; set; }
}

// mapping
public class MasterMap : EntityTypeConfiguration<Master>
{
    public MasterMap()
    {
        HasKey(t => t.Id);
        Property(t => t.Id)HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        ToTable("master", "public");
        Property(t => t.Id).HasColumnName("id");
    }
}

public class DetailMap : EntityTypeConfiguration<Master>
{
    public DetailMap()
    {
        HasKey(t => t.Id);
        Property(t => t.Id)HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        ToTable("detail", "public");
        Property(t => t.Id).HasColumnName("id");
        Property(t => t.MasterId).HasColumnName("master_id");
        HasRequired(t => t.Master)
            .WithMany(t => t.Details)
            .HasForeignKey(t => t.MasterId)
            .WillCascadeOnDelete(true);
    }
}
Error using this code:
var master = _repositoryMaster.Find(id);
_repositoryMaster.Delete(master);
_unitOfWork.SaveChanges();
Working properly on native EF (without repository pattern framework)
ctx.Master.Remove(id);
ctx.SaveChanges();
For this POC, I use:
  1. EF 6.0.0
  2. Repository Framework v3.3.5
  3. Npgsql.EntityFramework 2.2.3
  4. Npgsql 2.2.3
  5. Postresql 9.3
    Thanks in advance.
Coordinator
Feb 12, 2015 at 6:57 PM
Marked as answer by lelong37 on 2/12/2015 at 11:57 AM