user3566056
user3566056

Reputation: 244

entity framework code first inheritance property order in generated tables

I'm doing code first and since many of my POCO object share some properties, I decided to set up things with inheritance. Here are two of my data objects

public abstract class BaseDataObject
{
    [Key]
    public Guid Id {get; set;}

    public string Name {get; set;}
}

public class DataObject1: BaseDataObject
{
    public string SomeProperty {get; set;}
}

public class DataObject2: BaseDataObject
{
    public string OtherProperty {get; set;}
}

Now, if EF generates my tables, it'll create two tables with the following properties

DataObject1 Id (uniqueidentifier, PK) SomeProperty (nvarchar(max)) Name (nvarchar(max))

DataObject2 Id (uniqueidentifier, PK) OtherProperty (nvarchar(max)) Name (nvarchar(max))

So it puts the key first.. but the other property of my BaseDataObject is added at the end. I'd rather have those properties immediately after Id.. so the properties of BaseDataObject first, then the properties of the derived objects.

Is there a way to tell EF to do that when generating my tables? I know it's just cosmetics, but still... if somebody has a look at the DB directly, having those "base properties" at the end seems rather confusing.

Thanks.

Upvotes: 1

Views: 985

Answers (2)

user3566056
user3566056

Reputation: 244

I stumbled upon something when I had to create composite keys. it appears that the Order attribute in the Column property attribute can be used to ensure a certain order in the generated table.

Here's how I modified my BaseDataObject

public abstract class BaseDataObject
{
    [Key, Column(Order = 0)]
    public Guid Id {get; set;}

    [Column(Order = 1)]
    public string Name {get; set;}
}

This ensures that the Name column is at second position in the generated table, after any properties of DataObject1 and DataObject2.

Upvotes: 2

Stephen Brickner
Stephen Brickner

Reputation: 2602

You could do this with a mapping file. When you run the migration it will build the table in that order. That would require a mapping file for each entity. I don't know of a way you can set it up automatically in the migration tool without having a mapping file to set the order.

using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;

namespace YourNamespace.Mapping
{
    public class DataObject1Map : EntityTypeConfiguration<DataObject1>
    {
        public DataObject1Map()
        {
            // Primary Key
            this.HasKey(t => t.Id);

            // Properties
            this.Property(t => t.Name)
                .HasMaxLength(128);

            this.Property(t => t.SomeProperty)
                .IsRequired()
                .HasMaxLength(32);

            // Table & Column Mappings
            this.ToTable("DataObject1");
            this.Property(t => t.Id).HasColumnName("Id");
            this.Property(t => t.Name).HasColumnName("Name");
            this.Property(t => t.SomeProperty).HasColumnName("SomeProperty");
        }
    }
}

//In your context class
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
     modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

     modelBuilder.Configurations.Add(new DataObject1Map());

     base.OnModelCreating(modelBuilder);
}

Upvotes: 0

Related Questions