简体   繁体   中英

How do I use custom properties with the Entity Framework?

Lets say I have a form where a user can setup a custom object with a few drop downs, and then save that object. I am using the entity framework to handle the DAL. For example, they are setting up a profile and have to select a favorite color and I have a few colors in a drop down. The drop down is populated from a colors table. What If I want to add "Other" as an option and allow the user to type in a value. When I save the object, I grab the color Id from the drop down and save that to my profile object, but if they choose other, I want to save the contents of the text box instead. Is there a normal way of handling this? I don't want to re-invent the wheel here.

This is what I plan on trying, I will set the color id property of my profile to be a 0,1 relationship so it is not required, then I will add a partial profile class with a getter/setter for color name, in the getter, if the color object is not null i will return the name of the color, if it is null, i will return my custom property value.

The design as you describe would be create referential integrity issues. You are trying to create a field that is foreign key to the colors table and if that is not possible, the foreign key should point to another table.

A better design would be to let the user choose a color from the drop down, and if the color is not available, you provide the user with way to add the new color to the colors table maybe through some kind of a dialog. The color they add would then be selected in the dropdown.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM