EntiyFramework :Update model from database引起的两个问题
2013-04-18 18:52
459 查看
EntiyFramework一大特点就是Code first,但难免有时候因特殊原因需要Update model from database。此次使用该功能时遇到两个问题,且记之。
[问题一]
Error 3027: No mapping specified for the following EntitySet/AssociationSet - XXXXXXX.
[起因]
在数据库新建一张表,并使用Update model from database功能同步到EF, Add Association后,发生该错误。
[说明]
XXXXXXX代表关系名称,例如一个外键关系:FK_Bug_TestCase。
[原因]
在EF中Add Association时,.edmx文件并未被同步创建。
[解决方案]
Step1:右键.edmx文件,以XML Editor方式打开,如下图:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202003/21/b7d121215650361e4af90364d686e586.png)
Step2:ctrl+F 搜索,找到名称为XXXXXXX的Asscoiation节点,以FK_Bug_TestCase为例,并添加<ReferentialConstraint></ReferentialConstraint>节点,并设置Principal的Role属性为主键表名,子节点PropertyRef的Name属性为主键表用作外键的字段名;设置Dependent的Role属性为外键表名, 子节点PropertyRef的Name为外键字段名。如下图所示:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202003/21/1e40bc9272c102df87df43d67631d6cf.png)
Step3:保存即可。
[问题二]
Unable to update the EntitySet 'Bug' because it has a DefiningQuery and no <InsertFunction> element exists in the <ModificationFunctionMapping> element to support the current operation.
[起因]
在问题一已解决的前提下,对数据库进行更新是引起。
[原因]
使用Update model from database功能时,EF在.edmx文件里创建了< DefiningQuery ></ DefiningQuery >节点。DefiningQuery 节点的作用,MSDN解释如下:A defining query is commonly used to provide functionality similar to that provided by a database view, but the view is defined in the model, not the database.
[解决方案]
Step1:同问题一Step1,以XML Editor方式打开.edmx文件。
Step2:ctrl+F 搜索,找到名称为DefiningQuery的节点(其实一眼就可以看出来它所在EntitySet跟别的EntitySet的不同,用的时候就看到了),并删除。此外对其所在EntitySet节点的Store:Schema属性的Store:命名空间去掉,并删除Store:Name属性。如下图所示:
修改前:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202003/21/b4aa52986312415f37fde1d007be8add.png)
修改后:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202003/21/033e66c70c3a1da78a732c688c42d1e0.png)
Step3:保存即可。
[问题一]
Error 3027: No mapping specified for the following EntitySet/AssociationSet - XXXXXXX.
[起因]
在数据库新建一张表,并使用Update model from database功能同步到EF, Add Association后,发生该错误。
[说明]
XXXXXXX代表关系名称,例如一个外键关系:FK_Bug_TestCase。
[原因]
在EF中Add Association时,.edmx文件并未被同步创建。
[解决方案]
Step1:右键.edmx文件,以XML Editor方式打开,如下图:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202003/21/b7d121215650361e4af90364d686e586.png)
Step2:ctrl+F 搜索,找到名称为XXXXXXX的Asscoiation节点,以FK_Bug_TestCase为例,并添加<ReferentialConstraint></ReferentialConstraint>节点,并设置Principal的Role属性为主键表名,子节点PropertyRef的Name属性为主键表用作外键的字段名;设置Dependent的Role属性为外键表名, 子节点PropertyRef的Name为外键字段名。如下图所示:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202003/21/1e40bc9272c102df87df43d67631d6cf.png)
Step3:保存即可。
[问题二]
Unable to update the EntitySet 'Bug' because it has a DefiningQuery and no <InsertFunction> element exists in the <ModificationFunctionMapping> element to support the current operation.
[起因]
在问题一已解决的前提下,对数据库进行更新是引起。
[原因]
使用Update model from database功能时,EF在.edmx文件里创建了< DefiningQuery ></ DefiningQuery >节点。DefiningQuery 节点的作用,MSDN解释如下:A defining query is commonly used to provide functionality similar to that provided by a database view, but the view is defined in the model, not the database.
[解决方案]
Step1:同问题一Step1,以XML Editor方式打开.edmx文件。
Step2:ctrl+F 搜索,找到名称为DefiningQuery的节点(其实一眼就可以看出来它所在EntitySet跟别的EntitySet的不同,用的时候就看到了),并删除。此外对其所在EntitySet节点的Store:Schema属性的Store:命名空间去掉,并删除Store:Name属性。如下图所示:
修改前:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202003/21/b4aa52986312415f37fde1d007be8add.png)
修改后:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202003/21/033e66c70c3a1da78a732c688c42d1e0.png)
Step3:保存即可。
相关文章推荐
- PD Update Model from database, Unable to list the users.
- Entity Framework – Update Model From Database, Part 1
- Entity Framework – Update Model From Database, Part 1
- Entity Framework – Update Model From Database, Part 1
- Entity Framework – Update Model From Database, Part 2
- Entity Framework – Update Model From Database, Part 2
- Entity Framework – Update Model From Database, Part 2
- 解决Entity Framework Code First 的问题——Model compatibility cannot be checked because the database does not contain model metadata
- entityframework core database first update model
- android 数据库问题Can't downgrade database from version 3 to 1
- Allegro Update to Smooth出现 1 dynamic shape is still out of date or empty.Run out of date shape report from status dialog to identify them.对话框问题
- 关于两个表更新数据的问题(update)
- 关于引入两个jquery.js文件引起$符号命名冲突的问题
- mysql常见问题--target table for update in FROM clause
- psql遇到的两个问题 Cannot read termcap database和 free(): invalid next size
- 一个由两个长的如此相像的字引起的问题
- imagecreatefrompng函数引起的问题
- Mysql中,update语句引起的时间戳自动更新问题
- oracle实施问题Could not get a databaseId from dataSource
- 数据库连接问题之:Caused by: java.sql.SQLException: Connections could not be acquired from the underlying database!