In Entity Framework 4, I've got two tables in my database model (let's call them Info
and Data
) that map to actual database tables. In the Info
table, there is a DataId
column that links the two tables together (the tables are always 1:1).
Important columns on the table are:
Info :
Data :
I wrote a stored procedure to insert data. This procedure first inserts a row into Data
(so I can get the Data.id
value) and then inserts a row into Info
. It gets called in code when I call context.SaveChanges()
.
I set this stored procedure to run when inserting is done on XmlDataInfo
since it contains all the fields I need for the procedure except for Data.Xml
.
I'm running into two problems because of the Data.Xml
being in a different table...
If I do not set/change Data.Xml
, the INSERT
runs my stored procedure correctly. When I do set that value, the Entity Framework tries to do an insert on the Data
table too. I only want it to run my stored procedure call (on the Info
table) which inserts to both tables. I need to set Data.Xml
because my procedure needs this value so I can't leave it NULL.
I can't figure out how to set the Data.Xml
value in the stored procedure mapping. It doesn't let me select child elements for the property to use in the property drop down.
I saw some suggestions to create a partial class on Info
in code and I can do that to access this property in code but that partial class property is not available on the model so I still can't choose it as the mapping property.
That's the just of my issue. I haven't done a lot with EF yet so maybe there is something I'm missing or doing incorrectly.
It seems like there should be some way I can map a field on Info that can get/set Data.Xml
and then call my procedure without also trying to do an insert on the Data
table. I was hoping I could add some kind of code only property to the Info model that I could get this value with, but I can't figure out a way to do that where it is accessible in the stored procedure mappings.
It's hard to tell exactly what you are trying to do here. I think you may not have a complete understanding of EF or how stored procedures should be called.
A few things to note: 1. Your stored procedure should take care of inserting to both tables. When it is called from your C# code (via EF), you should not be able to tell which tables are being inserted into. 2. Your entities should include both tables if you intend to read/write data. If you include Data
in your entities, EF should automatically create a property for writing to Data.Xml
Now, let's assume that all of your table columns are varchars (strings) except for you Id's which are PKs (ints). Your stored procedure prototype would look like:
int MyStoredProcedure(string dataProperty1, string infoProperty1, string infoProperty2)
When you call this function from C# and then SaveChanges()
, the stored procedure would insert the strings into your table and would return to you the PK ( int
) of the record inserted into the Data
table. You can now use this value to insert your Data.Xml
at a later time.
entities.Data.Where(r => r.id == PK).First().Xml = "some XML data";
entities.SaveChanges();
This will grab the row from Data
where you just inserted (via the returned id
value) and then insert a value into the Xml
column.
Hopefully this clears things up a little bit. If you have a different structure/stored procedure, post some code samples for us to be able to give you more specific advice.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.