加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何在ADO.Net实体模型中将具有外键的表更新到另一个表?

发布时间:2020-12-15 07:42:56 所属栏目:百科 来源:网络整理
导读:我有2个表,Table1有一个主键’CustomizationId’,而Table2有一个与之匹配的FK Customizationid.表2没有主键. 我正在尝试从基于Web的表单添加新记录.我试图将其保存到数据库,我收到一个错误: Customization customization = new Customization(); Code code
我有2个表,Table1有一个主键’CustomizationId’,而Table2有一个与之匹配的FK Customizationid.表2没有主键.

我正在尝试从基于Web的表单添加新记录.我试图将其保存到数据库,我收到一个错误:

Customization customization = new Customization();
            Code code = new Code();
            customization.Name = CustomizationName.Text;               
            customization.LastUpdated = DateTime.Now;

            code.Top = top_js.InnerText;
            code.Bottom = bottom_js.InnerText;

            //code.CustomizationId = customization.CustomizationId;

            customization.Code = code;              
            entities.AddToCustomizations(customization);
            entities.SaveChanges();

当我调用SaveChanges时,无论是否添加注释行,我都会收到错误.

Unable to update the EntitySet 'Code' because it has a DefiningQuery and no <InsertFunction> element exists in the <ModificationFunctionMapping> element to support the current operation.

我该如何处理这种情况?我只想在我添加自定义的同时添加代码. “代码”表应将Customizationid设置为Customization设置的PK / Identity.

有任何想法吗?

解决方法

就EF而言,没有PK的表是View.

这意味着您有两种选择:

>讲一点白色谎言,并说服EF说‘view’ is a table
> Add modification functions(插入/更新/删除),以便您可以编辑它们.

通常修改函数是存储过程,但如果您无权访问数据库,则可以直接将T-SQL添加到SSDL中…

即< StorageModel>中的类似内容每个动作(插入,更新和删除)的EDMX元素:

<Function Name="InsertCode" BuiltIn="false" IsComposable="false" >
       <CommandText>
                INSERT dbo.TCode(ID,Name) VALUES (@ID,@Name)
       </CommandText>
       <Parameter Name="ID" Type="int" Mode="In" />
       <Parameter Name="ID" Type="nvarchar(max)" Mode="In" />
</Function>

在SSDL中具有修改功能后,需要map them,以便EF根据需要使用它们.

在你的情况下,我建议(1).

希望这可以帮助.

干杯亚历克斯

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读