即使定义了其他主键,Entity Framework 6也会创建Id列 [英] Entity Framework 6 creates Id column even though other primary key is defined

查看:74
本文介绍了即使定义了其他主键,Entity Framework 6也会创建Id列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将DataObject定义为:

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

并使用流畅的API使CompanyId和ServiceId成为复合密钥:

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

即使已经设置了主键,当我运行Add-Migration时,Entity Framework也会创建一个名为Id的列:

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

    }

如何防止EF添加此列?

How do I prevent EF from adding this column?

推荐答案

我怀疑这与您从 EntityData EntityData 具有名为 Id 的属性.我的猜测是EF变得困惑,因为有一个属性遵循它的键命名约定(即 Id )和显式定义的键.

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.

我怀疑您必须告诉它明确忽略 Id .

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

MSDN:EntityData类

更新:

我假设您正在为此使用Azure.这个 SO问题在答案中有一些其他信息,可能有助于您找到一个最佳解决方案.

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.

但是,我同意@Basic对您的问题的评论.由于它们引入的复杂性(和其他问题),我通常会避开带有EF的复合键.我怀疑您对 CompanyId ServiceId 字段的唯一约束将实现您想要的目标,而无需将它们包含在 SensorType 的主键中.这也意味着您可以只使用派生的 Id 属性作为主键,而避免整个问题.我不知道您的实施是否可行,但这是需要考虑的事情.

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.

这篇关于即使定义了其他主键,Entity Framework 6也会创建Id列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆