none
Private fields in entity RRS feed

  • Question

  • Hello

    In my edmx model, I have a few object: person, adress and aresstype. The person object has a number of fields: id, first name, last name, date of birht, date of death, cretated by, created date, changed by, changed date.... . I would like the end user to send in a person object with some of the fields populated, but some of the fields should not be public to the end user.

    Is it possible to use the objects created in the edmx file but set some of the fields to private? The "id", "created by", "created date", "changed by" and "changed date" should not be populated by the end user/front end, but should be populated by the mid tier. Some fields can also be set by the databse... "id" for examle

    Hope someone can help me!

    Thanks!

    Jules

    Monday, October 17, 2011 12:19 PM

Answers

  • Hi,

    Look up at points 8.4 and 8.5 of Entity Framework FAQ:

    http://www.ef-faq.org/entity-classes.html

     

    8.4. Does the Entity Framework require objects with public accessors for properties that are to be stored in the database? Does the Entity Framework support read-only properties?

     

    The system does not require public settable properties for all fields that are persisted to the database. While the default generated classes have public properties for each persisted field, you can either write your own classes or set the access for the getter and setter in the designer (public, internal, protected, or private). The Entity Framework does not, however, support properties which only have getters and no setters, and it does not support classes which have only fields and not properties.

     

    8.5. Does the Entity Framework support a private field to be mapped to the database?

     

    The system doesn't currently allow you to persist to a field directly. As noted above, however, you can simply make the generated property private. 

    Best regards,

    JA Reyes.

     


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solución de esta pregunta te ha sido útil.
    • Proposed as answer by tomigaspar Tuesday, October 18, 2011 8:07 AM
    • Marked as answer by Alan_chenModerator Monday, October 24, 2011 8:41 AM
    Monday, October 17, 2011 1:57 PM

All replies

  • Hi Jules;

    To your question, "Is it possible to use the objects created in the edmx file but set some of the fields to private?", doing so would cause operations to fail. For example lets say you have a table in the database called Person then when you querying the table Entity Framework would not be able to fill those fields as well and will throw an exception. It is better to create a class with those fields that you want the end user to see and have access to and when you want to send data to the end user create object of that type and when you receive that type of object from the user create a new or modify the Person object in the Object Context. This class that you would create is called a DTO, Data Transfer Object.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Monday, October 17, 2011 1:29 PM
  • Hi,

    Look up at points 8.4 and 8.5 of Entity Framework FAQ:

    http://www.ef-faq.org/entity-classes.html

     

    8.4. Does the Entity Framework require objects with public accessors for properties that are to be stored in the database? Does the Entity Framework support read-only properties?

     

    The system does not require public settable properties for all fields that are persisted to the database. While the default generated classes have public properties for each persisted field, you can either write your own classes or set the access for the getter and setter in the designer (public, internal, protected, or private). The Entity Framework does not, however, support properties which only have getters and no setters, and it does not support classes which have only fields and not properties.

     

    8.5. Does the Entity Framework support a private field to be mapped to the database?

     

    The system doesn't currently allow you to persist to a field directly. As noted above, however, you can simply make the generated property private. 

    Best regards,

    JA Reyes.

     


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solución de esta pregunta te ha sido útil.
    • Proposed as answer by tomigaspar Tuesday, October 18, 2011 8:07 AM
    • Marked as answer by Alan_chenModerator Monday, October 24, 2011 8:41 AM
    Monday, October 17, 2011 1:57 PM
  • Thanks all! Now I know how to code this. I also have some reading to do :)

     

    Cheers

    Julia

    Tuesday, October 18, 2011 5:02 AM