使用EF6/VS 2013,我创建了一个ADO.NET实体数据模型.此模型使用存储在SQL Server中的三个视图,以便从现有系统以适当的形式准备数据.一个用户(vw_Users),一个用于角色(vw_Roles)和交叉引用/关联视图(vw_UserRoles).
映射用户和角色实体时,应用程序将编译并执行而不会出现问题.但是,当我添加用户和角色之间的关联并将其映射到vw_UserRoles视图时,我得到一个内部异常的EntityCommandCompilationException,KeyNotFoundException {"字典中没有给定的密钥."}.
An exception of type 'System.Data.Entity.Core.EntityCommandCompilationException' occurred in mscorlib.dll but was not handled in user code
Additional information: An error occurred while preparing the command definition. See the inner exception for details.
Run Code Online (Sandbox Code Playgroud)
对我来说,这只是一个错误,但我是很多EF的新手,它可能与连接视图有关.出于兴趣,我实现了这个技巧,将视图视为一个表,但它没有任何区别.
更新#1:我使用新的SQL Server数据库创建了一个新的Web应用程序.我创建了基础表,其中包含关系和每个表的视图.当我基于表创建实体模型时,我没有任何问题 - 即使是关联.当我从视图中创建模型时,我得到了KeyNotFoundException.注意:我已更正了密钥,并在第二个基于视图的模型上添加并映射了关联.您可以从这里下载项目 - http://sdrv.ms/1eZ7ytp
更新#2:我已将此记录为MS @ Codeplex的问题 - https://entityframework.codeplex.com/workitem/1777
我仍然难以接受这一点,我真的很想用一系列观点完成这个.有什么想法吗?请....?
完全例外:
System.Data.Entity.Core.EntityCommandCompilationException was unhandled by user code
HResult=-2146232005
Message=An error occurred while preparing the command definition. See the inner exception for details.
Source=EntityFramework
StackTrace:
at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition..ctor(DbProviderFactory storeProviderFactory, DbCommandTree commandTree, …Run Code Online (Sandbox Code Playgroud)