创建一个新表Entity框架代码的第一个模式

时间:2012-12-11 21:25:26

标签: entity-framework ef-code-first ef-migrations

我对这个代码优先模式很新。 我创建了一个类,现在我想在我的数据库中创建一个表。 在PM控制台中,我编写了“添加迁移addSummaries”并点击回车。 我有以下错误:

PM> add-migration addsummaries
System.ArgumentNullException: Value cannot be null.
Parameter name: key
at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
at System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value)
at System.Data.Entity.ModelConfiguration.Configuration.Mapping.SortedEntityTypeIndex.Add(EdmEn    titySet entitySet, EdmEntityType entityType)
at  System.Data.Entity.ModelConfiguration.Configuration.Mapping.EntityMappingService.Analyze()
at  System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.ConfigureEntityTypes (DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest)
at     System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.Configure(DbDatabase Mapping databaseMapping, DbProviderManifest providerManifest)
at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest,  DbProviderInfo providerInfo)
at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext  internalContext)
at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.LazyInternalContext.get_CodeFirstModel()
at System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(DbContext context, XmlWriter  writer)
at System.Data.Entity.Migrations.Extensions.DbContextExtensions.<>c__DisplayClass1. <GetModel>b__0(XmlWriter w)
at System.Data.Entity.Migrations.Extensions.DbContextExtensions.GetModel(Action`1 writeXml)
at System.Data.Entity.Migrations.Extensions.DbContextExtensions.GetModel(DbContext context)
at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration  configuration, DbContext usersContext)
at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration  configuration)
at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.GetMigrator()
at     System.Data.Entity.Migrations.Design.ToolingFacade.GetPendingMigrationsRunner.RunCore()
at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()
Value cannot be null.
Parameter name: key
PM> 

我不知道这是什么意思。有人帮我找到实际错误吗?

编辑:

 public class Summary
{
    [Key]
    public int Id { get; set; }

    public Guid SummaryId { get; set; }

    [Required]
    [Display(Name="Title")]
    [MaxLength(500)]
    public string SummaryTitle { get; set; }

    [Display(Name = "Description")]
    public string Description { get; set; }

    public DateTime PublishDate { get; set; }

    public virtual UserProfile Writer { get; set; }

    public bool PublishStatus { get; set; }

    [Required]
    public DateTime LastActionDate { get; set; }

    public HttpPostedFileBase DocumentFile { get; set; }
    public string FileName { get; set; }
    public string FileLocation { get; set; }

    [Required]
    public string Tag { get; set; }

    public virtual ICollection<Course> Category { get; set; }
}

1 个答案:

答案 0 :(得分:2)

如果您有一个继承自“Summary”类的类,并且它引用了DbContext中未包含的另一个自定义类,则会遇到此问题。