[英]How do I get a a Model I made from the ADO.net entity model designed to only return data dependent on a foreign key
[英]How do you update a table with a foreign key to another table in ADO.Net Entity Model?
我有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。
这意味着您有两种选择:
通常修改函数是存储过程,但如果您无权访问数据库,则可以直接将T-SQL添加到SSDL中...
就是每个动作(插入,更新和删除)在EDMX的<StorageModel>
元素中都是这样的:
<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根据需要使用它们。
在你的情况下,我建议(1)。
希望这可以帮助。
干杯亚历克斯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.