entity-framework - Entity Framework 4.3.1 到 5.0 RC - 模型创建期间出现 NullReferenceException

标签 entity-framework entity-framework-5

我刚刚将我们的项目切换为尝试在 .NET 4.0 下运行的 Entity Framework 5.0 RC。除了更新命名空间引用之外,我们没有进行任何代码更改。第一次尝试访问 Entity Framework 对象时,我收到以下信息:

   at System.Data.Entity.ModelConfiguration.Conventions.ForeignKeyNavigationPropertyAttributeConvention.System.Data.Entity.ModelConfiguration.Conventions.IEdmConvention<System.Data.Entity.Edm.EdmNavigationProperty>.Apply(EdmNavigationProperty navigationProperty, EdmModel model)
   at System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.EdmConventionDispatcher.Dispatch[TEdmDataModelItem](TEdmDataModelItem item)
   at System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.EdmConventionDispatcher.VisitEdmNavigationProperty(EdmNavigationProperty item)
   at System.Data.Entity.Edm.Internal.DataModelItemVisitor.VisitCollection[T](IEnumerable`1 collection, Action`1 visitMethod)
   at System.Data.Entity.Edm.Internal.EdmModelVisitor.VisitDeclaredNavigationProperties(EdmEntityType entityType, IEnumerable`1 navigationProperties)
   at System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.EdmConventionDispatcher.VisitEdmEntityType(EdmEntityType item)
   at System.Data.Entity.Edm.Internal.DataModelItemVisitor.VisitCollection[T](IEnumerable`1 collection, Action`1 visitMethod)
   at System.Data.Entity.Edm.Internal.EdmModelVisitor.VisitEntityTypes(EdmNamespace edmNamespace, IEnumerable`1 entityTypes)
   at System.Data.Entity.Edm.Internal.EdmModelVisitor.VisitEdmNamespace(EdmNamespace item)
   at System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.EdmConventionDispatcher.VisitEdmNamespace(EdmNamespace item)
   at System.Data.Entity.Edm.Internal.DataModelItemVisitor.VisitCollection[T](IEnumerable`1 collection, Action`1 visitMethod)
   at System.Data.Entity.Edm.Internal.EdmModelVisitor.VisitNamespaces(EdmModel model, IEnumerable`1 namespaces)
   at System.Data.Entity.Edm.Internal.EdmModelVisitor.VisitEdmModel(EdmModel item)
   at System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.EdmConventionDispatcher.VisitEdmModel(EdmModel item)
   at System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.EdmConventionDispatcher.Dispatch()
   at System.Data.Entity.ModelConfiguration.Configuration.ConventionsConfiguration.ApplyModel(EdmModel model)
   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.InternalContext.Initialize()
   at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
   at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
   at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
   at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
   at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source, Expression`1 predicate)
   at BackEnd.Facade.Implementations.UserService.GetUserByUserName(String userName) in c:\Development\FundApps\Rapptr\src\FundApps.Rapptr.ServiceLayer\Facade\Implementations\UserService.cs:line 61
   at FundApps.Rapptr.Common.Web.Security.WebUserContextModule.OnPostAuthenticateRequest(HttpContextBase context) in c:\Development\FundApps\Rapptr\src\FundApps.Rapptr.Common.Web\Security\WebUserContextModule.cs:line 54
   at FundApps.Common.Mvc.BaseHttpModule.<Init>b__1(Object sender, EventArgs e) in c:\Development\FundApps\Rapptr\src\FundApps.Common.Mvc\BaseHttpModule.cs:line 14
   at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

有人对可能导致此问题的原因以及如何缩小范围有任何建议吗?

最佳答案

更新:此错误已在 EF5 RTM 中修复。

这是 EF5 中的一个错误。当您具有 1:1 关系并且还使用 ForeignKey 属性显式指定 FK 时,就会发生这种情况。例如:

public class OneToOneResult
{
    public int OneToOneResultId { get; set; }

    [ForeignKey("OneToOneResultId")]
    public virtual OneToOneResultDetail Detail { get; set; }
}

public class OneToOneResultDetail
{
   [Key]
   public int OneToOneResultId { get; set; }
   public DateTime DataDate { get; set; }
}

在 OnModelCreating 中:

modelBuilder.Entity<OneToOneResult>()
   .HasRequired(r => r.Detail)
   .WithRequiredPrincipal();

解决方法是删除 ForeignKey 属性。在这种情况下不需要它,因为 1:1 关系必须是 PK 到 PK,因为这就是 EF 在这种情况下支持的全部内容。

关于entity-framework - Entity Framework 4.3.1 到 5.0 RC - 模型创建期间出现 NullReferenceException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10918721/

相关文章:

database - 如何首先与 Entity Framework 代码热切加载多对多关系?

c# - 像 EntityFramework 6 中那样在 System.Data.Linq.DataContext 中记录执行时间

entity-framework-5 - EF 迁移 : Error changing an indexed field to nullable

c# - 我需要在 OnModelCreating(DbModelBuilder modelBuilder) 函数中添加什么来定义 Person 和 Role 之间的关系?

entity-framework - 如何找到 UIHInt 属性的目标属性?

entity-framework - 对数据库的更改已成功提交,但在更新对象上下文时出错

.net - MVC解决方案结构

c# - 如何在linq where条件中传递可选参数

entity-framework - CTP在 Entity Framework 中代表什么?

c# - MVC3/EF - 更改模型...数据库架构不匹配