This is a migrated thread and some comments may be shown as answers.

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

0 Answers 19 Views
Data Access Free Edition
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Jeffrey Monroe
Top achievements
Rank 2
Jeffrey Monroe asked on 30 Nov 2016, 12:11 AM

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
Tags
Data Access Free Edition
Asked by
Jeffrey Monroe
Top achievements
Rank 2
Share this question
or