问题
I was thinking that is there any way to determine the order of property loading in CodeFirst,for example i have a class like below:
public Class
{
public string Propert1{get;set;}
public string Propert2{get;set;}
public List<string> PropertList{get;set;}
}
And i need to make EF to load ProprtyList before property1!(Because i manipulate ProprtyList Values On Property1_Changed).
回答1:
A property should be just that: a property. I.e. you get or set it and nothing else, no side effects. I know that even some .Net classes that violate this rule, but still it is a very healthy principle. Another principle is that it should not matter in which order properties of an object are being set. That is because setting any individual property should leave an object in a valid state.
So consider your Class
object, the way EF materializes it, to be valid. Then you can start modifying it. If you want to modify an object in a way that multiple properties are changed simultaneously, you should do this by calling a method with a descriptive name, not by setting one property and silently changing others.
If you want a list that presents a different content based on PropertList
, make a read-only (unmapped) property or a GetXyz()
method in which the changed content is generated (without modifying PropertList
!) and returned.
回答2:
Then you can just declare PropertList as read only.
public Class
{
public string Propert1{get;set;}
public string Propert2{get;set;}
public List<String> PropertList{
get{
return genaratePropertyListFromProperty1();
}
}
}
Here prpertyList will be populated only when it is accessed.
来源:https://stackoverflow.com/questions/14191288/how-to-determine-the-order-of-property-loading-in-codefirst