Cascade Delete

4 posts, 0 answers
  1. Anton M.
    Anton M. avatar
    2 posts
    Member since:
    Jun 2011

    Posted 02 Feb 2013 Link to this post

    Hello.
    I'm using fluent mapping.

    I defined two classes:
      
    public class BaseContent
    {
        public BaseContent()
        {
            Parameters = new List<BaseParameter>();
        }
     
        public Guid Id { get; set; }
        public Guid? ParentId { get; set; }
        public int Number { get; set; }
        public string Name { get; set; }
        public bool IsWork { get; set; }
        public string NameOfWork { get; set; }
        public string Basis { get; set; }
        public string MeasureA { get; set; }
        public string MeasureB { get; set; }
        public bool IsChanged { get; set; }
     
        public IList<BaseParameter> Parameters { get; set; }
    }
     
    public class BaseParameter
    {
        public Guid Id { get; set; }
        public Guid WorkId { get; set; }
        public int Number { get; set; }
        public string Name { get; set; }
     
        public BaseContent Work { get; set; }
    }

    My mappings:
    var baseContentConfiguration = new MappingConfiguration<BaseContent>();
    baseContentConfiguration.MapType(x => new
        {
            x.Id,
            x.ParentId,
            x.Number,
            x.Name,
            x.IsWork,
            x.NameOfWork,
            x.Basis,
            x.MeasureA,
            x.MeasureB
        }).ToTable("BaseContents");
    baseContentConfiguration.HasProperty(x => x.Id).IsIdentity(KeyGenerator.Guid);
    baseContentConfiguration.HasProperty(x => x.Name).HasColumnType("nvarchar(MAX)");
    baseContentConfiguration.HasProperty(x => x.NameOfWork).HasColumnType("nvarchar(MAX)");
    baseContentConfiguration.HasProperty(x => x.Basis).HasColumnType("nvarchar(MAX)");
    baseContentConfiguration.HasProperty(x => x.MeasureA).HasColumnType("nvarchar(MAX)");
    baseContentConfiguration.HasProperty(x => x.MeasureB).HasColumnType("nvarchar(MAX)");
    baseContentConfiguration.HasAssociation(x => x.Parameters)
                            .WithOpposite(y => y.Work)
                            .HasConstraint((x, y) => x.Id == y.WorkId).IsManaged().IsDependent().IsRequired();
    configurations.Add(baseContentConfiguration);
     
    var baseParameterConfiguration = new MappingConfiguration<BaseParameter>();
    baseParameterConfiguration.MapType(x => new
        {
            x.Id,
            x.WorkId,
            x.Number,
            x.Name
        }).ToTable("BaseParameters");
    baseParameterConfiguration.HasProperty(x => x.Id).IsIdentity(KeyGenerator.Guid);
    baseParameterConfiguration.HasProperty(x => x.WorkId).IsNotNullable();
    baseParameterConfiguration.HasProperty(x => x.Name).HasColumnType("nvarchar(MAX)");
    configurations.Add(baseParameterConfiguration);

    Foreign keys will appear in base, but without cascade delete for Parameters.
    Tell me please what I'm doing wrong?

  2. Alexander
    Admin
    Alexander avatar
    727 posts

    Posted 06 Feb 2013 Link to this post

    Hello Anton,

    I tried your code and it works as expected - all related BaseParameter instances are deleted together with the deleted BaseContent object.
    Could you please elaborate a bit more on the behavior you observe on your side? If possible, please also include the code that you have and clarify where exactly it behaves differently than what you expect.

    Regards,
    Alexander
    the Telerik team
    Q3'12 SP1 of OpenAccess ORM packs Multi-Table Entities mapping support. Check it out.
  3. DevCraft banner
  4. Anton M.
    Anton M. avatar
    2 posts
    Member since:
    Jun 2011

    Posted 06 Feb 2013 Link to this post

    Hello. Thanks for reply. I'm using MS SQL 2008R2 as database. I open SQL Server Management Studio and I see that created by OpenAccess foreign key for table "BaseParameters" has no cascade delete in properties
  5. Alexander
    Admin
    Alexander avatar
    727 posts

    Posted 11 Feb 2013 Link to this post

    Hi Anton,

    You are right, currently the foreign key constraints created on the database server by OpenAccess do not have the cascading delete option enabled. This functionality is entirely handled by the runtime of OpenAccess when it is enabled in your persistent model.

    If this is important to you, please feel free to add an entry to our Ideas & Feedback portal.

    Regards,
    Alexander
    the Telerik team
    Q3'12 SP1 of OpenAccess ORM packs Multi-Table Entities mapping support. Check it out.
Back to Top