调用Enable-Migrations -Force时,KeyAttribute不在EF6 Code First上工作

时间:2015-03-25 14:46:42

标签: c# entity-framework ef-migrations

我尝试启动EF6 Code First项目,即使我的所有实体都指定了Key属性,但在执行命令Enable-Migrations -Force时仍无法识别某些实体。

以下是一个例子:


    [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]属性的原因的任何提示?还有一些其他类型,所有这些类型都使用复合键,这也会发生,但对于其他类型,也使用复合键,该属性可以正常工作。

备注:此代码由自定义工具生成,但它会编译。此外,删除显式接口实现也无济于事。

0 个答案:

没有答案