The day before yesterday, I made an MVC Entity FrameWork project and encountered a data editing problem with foreign key Association. When you edit the data, follow the normal logic and place the data in each field into the seat. Then, click the Save button. In this case, the data will be edited in a simple way, but EF is different, it is not as good as you want. When you view the data list, you will find another piece of data in the list inexplicably. It hurts a lot.
Entity class
ID {;}deviceview DeviceView {;}devicesensorpoint DeviceSensorPoint {;}viewcontrolclassname {;}viewcontrolposition {;} Remark {;;}}Object Class 1 (primary key table) Device deviceID {;}id {;}code {;}viewname {;;}}Entity Class 2 (foreign key table) ID {; Device deviceID {;}englishname {;}oldenglishname {;} ChineseName {;} ValueType {;;} nullable <> Readable {;}nullable <> Writeable {;}nullable <> RecommandValue {;}nullable <> WriteValue {;}nullable <> IsWriten {;;} nullable <> PLCReadChannelNo {;}nullable <> PLCWriteChannelNo {;;} SwitchNameOfTrue {;} SwitchNameOfFalse {;}nullable <> MaxValue {;;} nullable <> MinValue {;}isalarmneeded {;}unit {;;}nullable <> AlarmUpperValue {;}nullable <> AlarmLowerValue {;}alarmupperinfo {;;} alarmLowerInfo {;}remark {;}nullable <> CurrentValue {;Entity Class 3 (foreign key table)
Implementation Method:
The original implementation is to directly assign the ID (foreign key Association) to the foreign key, but this is not possible. The effect described at the beginning will appear. I thought of the following solution.
ActionResult Edit (deviceViewCode, DAL. DeviceViewControl deviceViewControl ,(! (ModelState. addModelError (, deviceView =. db. deviceViews. where (p => p. code = deviceSeneorPoint = db. deviceSensorPoints. where (p => p. ID = deviceControl = db. deviceViewControls. where (p => p. ID = deviceControl. remark = deviceControl. viewControlClassName = deviceControl. deviceSensorPoint = RedirectToAction (, {deviceViewCode =}Implement editing
Solution: first obtain the object of the foreign key table and assign the object data to the foreign key of the table to be edited. In this way, the primary and foreign key associations are implemented. Editing is implemented, rather than editing and adding.