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

How do you update a table with a foreign key to another table in ADO.Net Entity Model?(如何将具有外键的表更新到 ADO.Net 实体模型中的另一个表?)
本文介绍了如何将具有外键的表更新到 ADO.Net 实体模型中的另一个表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个表,Table1 有一个主键CustomizationId",Table2 有一个与之匹配的 FK Customizationid.Table2 没有主键.

I have 2 tables, Table1 has a primary key 'CustomizationId', and Table2 has a FK Customizationid which matches this. Table2 has no primary key.

我正在尝试从基于 Web 的表单中添加新记录.我尝试将其保存到数据库中,但出现错误:

I am trying to add a new record from a web based form. I attempt to save this to the database and I get an error:

            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 时,我收到一个错误,无论我是否在注释行中添加.

When I call SaveChanges I am getting an error, whether or not I add in the commented line.

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.

How do I handle this situation? I just want to add in the code at the same time that I add in the Customization. The 'Code' table should have the Customizationid set to the PK/Identity set by Customization.

有什么想法吗?

推荐答案

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

As far as the EF is concerned the table without a PK is a View.

这意味着您有两个选择:

This means you have two options:

  1. 说一个善意的谎言,并让 EF 相信 'view' 是一个表格
  2. 添加修改功能(插入/更新/删除)以便您可以编辑它们.
  1. Tell a little white lie, and convince the EF that the 'view' is a table
  2. Add modification functions (Insert/Update/Delete) so you can edit them.

一般修改函数都是存储过程,但是如果你没有访问数据库的权限,其实可以直接将T-SQL添加到SSDL中...

Generally modification functions are stored procedures, but you can actually add T-SQL directly to the SSDL if you don't have access to the database...

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

I.e. something like this in the <StorageModel> element of the EDMX for each action (insert, update and delete):

<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 中拥有修改功能后,您需要映射它们 以便 EF 根据需要使用它们.

Once you have the modification functions in your SSDL you need to map them so that the EF uses them as required.

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

In your situation I recommend (1).

希望这会有所帮助.

干杯亚历克斯

这篇关于如何将具有外键的表更新到 ADO.Net 实体模型中的另一个表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)