Emil Nyborg
Emil Nyborg

Reputation: 73

Entity Framework 6 creates Id column even though other primary key is defined

I defined a DataObject as:

public class SensorType : EntityData
{
    //PKs
    public string CompanyId { get; set; }
    public string ServiceId { get; set; }

    public string Type { get; set; }
}

And used fluent API to make CompanyId and ServiceId a composite key:

modelBuilder.Entity<SensorType>()
            .HasKey(t => new { t.CompanyId, t.ServiceId });

//No autogeneration of PKs
modelBuilder.Entity<SensorType>().Property(t => t.ServiceId)
            .HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.None);
modelBuilder.Entity<SensorType>().Property(t => t.CompanyId)
            .HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.None);

Even though a Primary Key has been set Entity Framework creates a column named Id when I run Add-Migration:

CreateTable(
            "dbo.SensorTypes",
            c => new
                {
                    CompanyId = c.String(nullable: false, maxLength: 128),
                    ServiceId = c.String(nullable: false, maxLength: 128),
                    Type = c.String(),
                    Id = c.String(
                        annotations: new Dictionary<string, AnnotationValues>
                        {
                            { 
                                "ServiceTableColumn",
                                new AnnotationValues(oldValue: null, newValue: "Id")

                   ...
                })
            .PrimaryKey(t => new { t.CompanyId, t.ServiceId })
            .Index(t => t.CreatedAt, clustered: true);

    }

How do I prevent EF from adding this column?

Upvotes: 5

Views: 2195

Answers (2)

John Laffoon
John Laffoon

Reputation: 2915

I suspect it has something to do with the fact that you are deriving your class from EntityData and EntityData has a property named Id. My guess is that EF is getting confused because there is a property which adheres to it's key naming conventions (i.e. Id) and an explicitly defined key.

I suspect you'll have to tell it to explicitly ignore Id.

MSDN: EntityData Class

UPDATE:

I'm assuming that you're working with Azure for this. This SO question has some additional information in the answers which may help you find an optimal solution.

However, I agree with @Basic in his comment to your question. I generally shy away from composite keys with EF due to the complexity (and other issues) they introduce. I suspect a unique constraint on your CompanyId and ServiceId fields will achieve what you want without involving them in the primary key for SensorType. That also means that you can just use the derived Id property as your primary key and avoid the entire issue all together. I don't know if it is feasible for your implementation, but it is something to consider.

Upvotes: 3

Leonardo Ramos Duarte
Leonardo Ramos Duarte

Reputation: 292

See text in documentation:

Entity Framework relies on every entity having a key value that it uses for tracking entities. One of the conventions that code first depends on is how it implies which property is the key in each of the code first classes. That convention is to look for a property named “Id” or one that combines the class name and “Id”, such as “BlogId”. The property will map to a primary key column in the database.

to use a different key, you need to use the annotations [key], ex:

[Key] 
public int primaryKey { get; set; } 

Documentation Oficial

Tutorial Link

Upvotes: -1

Related Questions