我所定义的数据对象作为Id列:实体框架6创建即使其他主键被定义
public class SensorType : EntityData
{
//PKs
public string CompanyId { get; set; }
public string ServiceId { get; set; }
public string Type { get; set; }
}
并用于流利API做出CompanyId和SERVICEID复合键:
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的列:
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添加此列?
是否故意尝试将两个字段的组合作为复合键使用?如果是这样,你有一个很好的理由吗? – Basic
你不需要告诉实体框架关于DatabaseGeneratedOption。没有,只是设置与公司和服务表的关系 – Monah
放置EntityData的代码,似乎实体数据包含Id,这就是使其成为主键的原因 – Monah