Entity Framework 6 creates an identity column, although a different primary key is defined

I defined the DataObject as:

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

    public string Type { get; set; }
}

And used the free API to make CompanyId and ServiceId composite:

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);

Although the primary key is installed, the Entity Framework creates a column called Id when Add-Migration starts:

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 to prevent EF from adding this column?

+4
source share
2 answers

, , EntityData EntityData Id. , EF , , (.. Id) .

, , Id.

MSDN: EntityData

UPDATE:

, Azure . fooobar.com/questions/1655062/... , .

@Basic . EF - ( ), . , CompanyId ServiceId , , SensorType. , Id . , , , .

+2

. :

Entity Framework , , . , - , , . "Id" , "Id", "BlogId". .

, [key], :

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

-1

Source: https://habr.com/ru/post/1655061/


All Articles