Setting vertical inheritance on class causes underlying child table to have all columns set to null

Thread is closed for posting
1 posts, 0 answers
  1. Jeffrey Monroe
    Jeffrey Monroe avatar
    7 posts
    Member since:
    Feb 2005

    Posted 29 Nov 2016 Link to this post

    When setting inheritance for a class, the column nullability and associations are ignored. I believe this is a function of setting inheritance, but this is a bug I would suspect.

     

    For example, I have a class defined with vertical inheritance as defined below:

    public partial class ObjectKey : ObjectEntity {
     
        public ObjectKey()
        {
            OnCreated();
        }
     
        public virtual int KeyTypeId
        {
            get;
            set;
        }
     
        public virtual System.Guid SourceObjectId
        {
            get;
            set;
        }
     
        public virtual string Name
        {
            get;
            set;
        }
     
        public virtual string Value
        {
            get;
            set;
        }
     
        public virtual SystemType KeyType
        {
            get;
            set;
        }
     
        public virtual ObjectEntity SourceObject
        {
            get;
            set;
        }
     
        #region Extensibility Method Definitions
     
        partial void OnCreated();
         
        #endregion
    }

     

    The MetadataSource is defined as:

    public partial class ModelMetadataSource
    {
        public MappingConfiguration<ObjectKey> GetObjectKeyMappingConfiguration()
        {
            MappingConfiguration<ObjectKey> configuration = this.GetObjectKeyClassConfiguration();
            this.PrepareObjectKeyConfigurations(configuration);
            this.OnPrepareObjectKeyConfigurations(configuration);
            return configuration;
        }
     
        public MappingConfiguration<ObjectKey> GetObjectKeyClassConfiguration()
        {
            MappingConfiguration<ObjectKey> configuration = new MappingConfiguration<ObjectKey>();
            configuration.MapType(x => new { }).UpdateSchema(true).Inheritance(Telerik.OpenAccess.InheritanceStrategy.Vertical).ToTable("ipObjects.ObjectKey");
            return configuration;
        }
     
        public void PrepareObjectKeyConfigurations(MappingConfiguration<ObjectKey> configuration)
        {
            configuration.HasIdentity().ToColumn(@"ObjectEntityId");
            configuration.HasProperty(x => x.KeyTypeId).ToColumn(@"KeyTypeId").WithOpenAccessType(OpenAccessType.Int32).IsNotNullable();
            configuration.HasProperty(x => x.SourceObjectId).ToColumn(@"SourceObjectId").WithOpenAccessType(OpenAccessType.Guid).IsNotNullable();
            configuration.HasProperty(x => x.Name).ToColumn(@"Name").WithOpenAccessType(OpenAccessType.UnicodeStringVariableLength).IsNotNullable().HasLength(64).IsNotUnicode();
            configuration.HasProperty(x => x.Value).ToColumn(@"Value").WithOpenAccessType(OpenAccessType.UnicodeStringVariableLength).IsNotNullable().HasLength(255).IsNotUnicode();
            configuration.HasAssociation<SystemType>(x => x.KeyType).HasConstraint((x, y) =>  x.KeyTypeId == y.SystemTypeId);
            configuration.HasAssociation<ObjectEntity>(x => x.SourceObject).HasConstraint((x, y) =>  x.SourceObjectId == y.ObjectEntityId).IsDependent();
        }
     
        partial void OnPrepareObjectKeyConfigurations(MappingConfiguration<ObjectKey> configuration);
    }

     

    Even though the properties as set as not nullable, the database columns are defined as nullable when updating the database schema from the model. Furthermore, the associations are note created as constraints. Here is the DDL that gets generated:

    CREATE TABLE [ipObjects].[ObjectKey] (
        [ObjectEntityId] uniqueidentifier NOT NULL,
        [Name] nvarchar(64) NULL,               -- ObjectKey.<Name>k__BackingField
        [Value] nvarchar(255) NULL,             -- ObjectKey.<Value>k__BackingField
        [KeyTypeId] int NULL,                   -- ObjectKey.<KeyType>k__BackingField
        [SourceObjectId] uniqueidentifier NULL, -- ObjectKey.<SourceObject>k__BackingField
        CONSTRAINT [pk_ObjectKey] PRIMARY KEY ([ObjectEntityId])
    )
     
    go
     
    ALTER TABLE [ipObjects].[ObjectKey] ADD CONSTRAINT [ref_ObjectKey_ObjectEntity] FOREIGN KEY ([ObjectEntityId]) REFERENCES [ipObjects].[ObjectEntity]([ObjectEntityId])
     
    go
Back to Top