How to update a table with a foreign key in another table in the Entity Model ADO.Net?

I have 2 tables, table 1 has the primary key "CustomizationId", and Table2 has an FK Customizationid that matches this. Table 2 does not have a primary key.

I am trying to add a new entry from a web form. I try to save this in a 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(); 

When I call SaveChanges, I get an error message, regardless of whether I add to 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. 

How do I deal with this situation? I just want to add the code at the same time as in the setup. In the Code table, the Customizationid parameter for PK / Identity must be set and configured.

Any ideas?

+7
c # entity-framework
source share
1 answer

As for EF, a table without PK is a view.

This means that you have two options:

Usually, modification functions are stored procedures, but you can really add T-SQL directly to SSDL if you do not have access to the database ...

those. something like this in the <StorageModel> element for 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> 

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

In your situation, I recommend (1).

Hope this helps.

Greetings Alex

+7
source share

All Articles