0

I have a class that have all properties private to set:

public class MyClass
{
   public int Id {get;private set;}
   public int IdChild {get;private set;}
   public string SomeProperty {get;private set;}

   public virtual Child Child {get;private set;}

}

So, to modify their properties, i have to: (by example)

1) Call method to modify the property:

myclass.SetChild(child);

So, how I can create an entity (I´m using EF Code First) with a Dynamic Proxy?

var myClass = myContext.MyClass.Create();
myClass.Id = 1 // Cannot do that, because this property is "Private Set"

PS: I already have ProxyCreation and LazyLoad enabled!

GuFigueiredo
  • 635
  • 1
  • 9
  • 18
  • 1
    First, the method is not a constructor. Then you have Create method which looks like actual factory method. Then you mention dynamic proxy while it sounds like you are misusing this notion. And last, you try to set a property that has a private setter but it doesn't seem related to proxy but rather to the property. So much confusion in such short question. – Wiktor Zychla Apr 25 '14 at 19:19
  • The fact is that I have a object that cannot exist in a invalid state. For other properties it's fine to change values, but for the ID, I cannot do that without expose a method that changes this property, that actually is not a good idea. – GuFigueiredo Apr 25 '14 at 19:52
  • And why would you like to change id programatically if it is auto generated upon inserting? – Wiktor Zychla Apr 25 '14 at 19:54
  • To create a Dynamic Proxy, that uses the entity key to retrieve the relationships. See: http://stackoverflow.com/questions/7311949/ramifications-of-dbset-create-versus-new-entity – GuFigueiredo Apr 25 '14 at 20:15
  • Ok, we finally get somewhere. So what keeps you from having both getter and setter public? – Wiktor Zychla Apr 25 '14 at 20:28
  • 1
    http://msdn.microsoft.com/en-us/library/vstudio/dd468057%28v=vs.100%29.aspx – Gert Arnold Apr 25 '14 at 21:41

0 Answers0