orm is dependent

2 posts, 0 answers
  1. Chom
    Chom avatar
    56 posts
    Member since:
    Dec 2009

    Posted 08 Feb 2013 Link to this post

    Hello,
    I am using the ORM fluent mapping and I have managed to create my schema etc and have just figured out the associations and got that working last night. I am still struggling to figure out the IsDependent() so that I can set up cascade deletes.

    I have tried several things and am not sure if I am getting it or if I am missing something. My intepretation of cascade deletes is that I usually set it up in the Foreign Key Relationships dialog in management studio. So I am looking for the Cascade Delete to be set to true after executing my fluent code. Is that the case or am I looking in the wrong direction?

    My classes are as follows:
    internal class DefaultMappingDataSource : FluentMetadataSource
    {
        //In order to generate DDL script for creating constraints in the database, you should override the CreateModel method.
        protected override Telerik.OpenAccess.Metadata.MetadataContainer CreateModel()
        {
            Telerik.OpenAccess.Metadata.MetadataContainer model = base.CreateModel();
            model.DefaultMapping.NullForeignKey = true;
            return model;
        }
     
        protected override IList<MappingConfiguration> PrepareMapping()
        {         
            //Define mapping configuration.
            var mappingConfig = new List<MappingConfiguration>();
     
            //Call mapping config method entities.
            var tagBankConfig = PrepareTagBankMapping();
            var abLogix5000Config = PrepareABLogix5000Mapping();
            var modbusConfig = PrepareModbusMapping();
     
            //Set upp associations.
            tagBankConfig
                .HasAssociation(e => e.ABLogixAssociation)
                .WithOpposite(p => p.TagBankAssociation)
                .HasConstraint((e, p) => e.TagBankId == p.ABLogix_TagBankId)
                .IsManaged()
                .IsDependent(true)
                .IsRequired();
     
            //abLogix5000Config
            //    .HasAssociation(e => e.TagBankAssociation)
            //    .WithOpposite(p => p.ABLogixAssociation)
            //    .HasConstraint((e, p) => e.ABLogix_TagBankId == p.TagBankId)
            //    .IsManaged()
            //    .IsDependent(true)
            //    .IsRequired();
                 
            //Add the table entities to the configuration.
            mappingConfig.Add(tagBankConfig);
            mappingConfig.Add(abLogix5000Config);
            mappingConfig.Add(modbusConfig);
     
            return mappingConfig;
        }
     
        private MappingConfiguration<Tags_TagBank> PrepareTagBankMapping()
        {
            //Create mapping configuration for table columns/properties.
            var config = new MappingConfiguration<Tags_TagBank>();
     
            //Map/create table.
            config.MapType(x => new
            {
                TagBankId = x.TagBankId,
                Bank = x.Name
            }).ToTable("Banks");
            config.HasProperty(bank => bank.TagBankId).IsIdentity(KeyGenerator.Autoinc);
            config.HasProperty(bank => bank.Name).IsNullable().IsUnicode().HasLength(50);
     
            return config;
        }
     
        private MappingConfiguration<Tags_ABLogix5000> PrepareABLogix5000Mapping()
        {
            //Create mapping configuration for table columns/properties.
            var config = new MappingConfiguration<Tags_ABLogix5000>();
     
            //Map/create table.
            config.MapType(x => new
            {
                Id = x.TagId,
                ABLogix_TagBankId = x.ABLogix_TagBankId,
                TagName = x.TagName
            }).ToTable("TagsABLogix5000");
            config.HasProperty(ablogix => ablogix.TagId).IsIdentity(KeyGenerator.Autoinc);
            config.HasProperty(ablogix => ablogix.ABLogix_TagBankId).IsNotNullable();
     
            return config;
        }

    public class Tags_ABLogix5000
    {
        //Mapped properties.
        public int TagId { get; set; }
        public int ABLogix_TagBankId { get; set; }
        public string TagName { get; set; }
     
        //Associations
        public Tags_TagBank TagBankAssociation { get; set; }
    }


    public class Tags_TagBank
    {
        public Tags_TagBank()
        {
            this.ABLogixAssociation = new List<Tags_ABLogix5000>();
        }
     
        //Mapped properties.       
        public int TagBankId { get; set; }
        public string Name { get; set; }
     
        //Associations
        public IList<Tags_ABLogix5000> ABLogixAssociation { get; set; }
    }


    I have tried using the tagbankconfig and the ablogix5000 config and see no visible result.

    I would appreciate some help to solve my dilema with this issue.

    Thanks

    chom




     

  2. PetarP
    Admin
    PetarP avatar
    754 posts

    Posted 13 Feb 2013 Link to this post

    Hi Gavin,

     Whenever you want to specify cascading delete that must be done on the collection side of your association. If you specify it on the one side it will not work and will even ignore the setting on your collection side (as this is wrong setup).
    If you only specify it on your collection side it should work as expected.

    Greetings,
    Petar
    the Telerik team
    Q3'12 SP1 of OpenAccess ORM packs Multi-Table Entities mapping support. Check it out.
  3. DevCraft banner
Back to Top