C# 调用Enable Migrations-Force时KeyAttribute不首先处理EF6代码
我正在尝试启动一个EF6 Code First项目,尽管我的所有实体都指定了Key属性,但在执行命令Enable Migrations-Force时,其中一些实体无法识别 以下是一个例子:C# 调用Enable Migrations-Force时KeyAttribute不首先处理EF6代码,c#,entity-framework,entity-framework-migrations,C#,Entity Framework,Entity Framework Migrations,我正在尝试启动一个EF6 Code First项目,尽管我的所有实体都指定了Key属性,但在执行命令Enable Migrations-Force时,其中一些实体无法识别 以下是一个例子: [DataContract(IsReference = true)] public class StreetAddress : CIM.Data.IEC61968.Common.IStreetAddress { [DataMember] [Key]
[DataContract(IsReference = true)]
public class StreetAddress : CIM.Data.IEC61968.Common.IStreetAddress
{
[DataMember]
[Key]
[Column(Order=0)]
public CIM.Data.EF.Status status { get; set; }
CIM.Data.IEC61968.Common.IStatus CIM.Data.IEC61968.Common.IStreetAddress.status
{
get { return (CIM.Data.IEC61968.Common.IStatus)status; }
set { status = (CIM.Data.EF.Status)value; }
}
[DataMember]
[Key]
[Column(Order=1)]
public CIM.Data.EF.StreetDetail streetDetail { get; set; }
CIM.Data.IEC61968.Common.IStreetDetail CIM.Data.IEC61968.Common.IStreetAddress.streetDetail
{
get { return (CIM.Data.IEC61968.Common.IStreetDetail)streetDetail; }
set { streetDetail = (CIM.Data.EF.StreetDetail)value; }
}
[DataMember]
[Key]
[Column(Order=2)]
public CIM.Data.EF.TownDetail townDetail { get; set; }
CIM.Data.IEC61968.Common.ITownDetail CIM.Data.IEC61968.Common.IStreetAddress.townDetail
{
get { return (CIM.Data.IEC61968.Common.ITownDetail)townDetail; }
set { townDetail = (CIM.Data.EF.TownDetail)value; }
}
}
这是命令的输出,为了简洁起见,有一些剪切:
PM> Enable-Migrations -Force
Checking if the context targets an existing database...
System.Data.Entity.ModelConfiguration.ModelValidationException: One or more validation errors were detected during model generation:
CIM.Data.EF.StreetAddress: : EntityType 'StreetAddress' has no key defined. Define the key for this EntityType.
//... same message for a few other types ...
StreetAddresss: EntityType: EntitySet 'StreetAddresss' is based on type 'StreetAddress' that has no keys defined.
//... same message for a few other collections ...
at System.Data.Entity.Core.Metadata.Edm.EdmModel.Validate()
//... some intermediate stack trace ...
at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
One or more validation errors were detected during model generation:
CIM.Data.EF.StreetAddress: : EntityType 'StreetAddress' has no key defined. Define the key for this EntityType.
//... same message for a few other types ...
StreetAddresss: EntityType: EntitySet 'StreetAddresss' is based on type 'StreetAddress' that has no keys defined.
//... same message for a few other collections ...
关于[Key]属性被忽略的原因有什么提示吗?还有一些其他类型,所有这些类型都具有复合关键点,这种情况也会发生,但对于其他类型,也具有复合关键点,属性起作用
备注:此代码由自定义工具生成,但可编译。此外,删除显式接口实现也无济于事。您的问题很可能是因为您在主键中使用了复合对象。也许你最好使用Guid或int。我也这么认为,但我正在实现一个无法更改的外部契约。这些案例是组合,因此我将尝试以不同的方式对这些实体进行建模,保留接口的实现。既然您首先使用代码,是什么阻止您添加额外的id字段并将其用作pk?@Hintham我现在正在这样做。问题确实在于复合对象。无论如何,它们不应该存储在单独的表中。它们是值对象,而不是DDD术语中的实体,应该将其字段直接包含在由它们组成的实体表中。