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

ぐ巨炮叔叔 提交于 2021-01-27 05:30:42

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/39573529/entity-framework-6-creates-id-column-even-though-other-primary-key-is-defined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!