Entity FrameWork adds a data table associated with a foreign key

Source: Internet
Author: User

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.

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.