它有一个 DefiningQuery 但没有 InsertFunction 元素......错误 [英] It has a DefiningQuery but no InsertFunction element... err

查看:26
本文介绍了它有一个 DefiningQuery 但没有 InsertFunction 元素......错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这件事让我发疯,错误对我来说毫无意义:

无法更新 EntitySet 'TableB',因为它有一个 DefiningQuery 并且元素中不存在支持当前操作的元素.

我的桌子是这样摆放的:

<前>表Aint idA(身份,主键)...表Bint idA(TableA.idA 的 FK)整数值

TableB 在 SQL 服务器中没有定义的主键.实体框架已导入表和关联并将这两个字段设置为键.但是当我尝试插入表时它会输出该错误!

怎么了??

<小时>

正如亚历克斯所建议的,解决方案是这样的:

  1. 右键单击 edmx 文件,选择打开方式,XML 编辑器
  2. 在 edmx:StorageModels 元素中找到实体
  3. 完全删除 DefiningQuery
  4. 将 store:Schema="dbo" 重命名为 Schema="dbo"(否则代码会报错,说名称无效)
  5. 移除 store:Name 属性

我保留了密钥,因为我认为两列都是密钥的一部分.

解决方案

好吧,当遇到没有 PrimaryKey 的表时,它被视为一个视图.

视图显示在 StorageModelEntitySet[n]DefiningQuery 元素中的 EDMX 文件(在 XML 编辑器中打开以查看)中.

当您有 DefiningQuery 时,实体将变为只读,除非您添加修改功能.您需要 3 个修改函数(又名存储过程),分别用于插入、更新和删除.

但是你有两个选择:

更改密钥定义:

  1. 并让 EF 相信它认为的视图实际上是一个表
  2. 或者添加相应的修改功能

在你的情况下,我推荐 (1).

This thing is driving me crazy, and the error is quite meaningless to me:

Unable to update the EntitySet 'TableB' because it has a DefiningQuery and no element exists in the element to support the current operation.

My tables are put like this:

TableA
int idA (identity, primary key)
...

TableB
int idA (FK for TableA.idA)
int val

TableB has no defined primary key in the SQL server. The Entity Framework has imported the table and the association and set both fields as key. But it will output that error when I try to do an insert into the table!

What's wrong??


Edit: As suggested by Alex, the solution was this:

  1. Right click on the edmx file, select Open with, XML editor
  2. Locate the entity in the edmx:StorageModels element
  3. Remove the DefiningQuery entirely
  4. Rename the store:Schema="dbo" to Schema="dbo" (otherwise, the code will generate an error saying the name is invalid)
  5. Remove the store:Name property

I left the key as it was, since it was OK to me that both the columns are part of the key.

解决方案

Well when a table is encountered without a PrimaryKey it is treated as a View.

And views show up in the EDMX file (open in an XML editor to see) in the StorageModelEntitySet[n]DefiningQuery element.

When you have a DefiningQuery the Entity becomes readonly unless you add modification functions. You need 3 modifications functions (aka Stored Procedures) one for each of Insert, Update and Delete.

But you have two options:

Change the key definion:

  1. And convince the EF that what it thinks is a view is really a table
  2. Or add the appropriate modification functions

In your case I recommend (1).

这篇关于它有一个 DefiningQuery 但没有 InsertFunction 元素......错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆