locked
Create Method in Entity Framwork RRS feed

  • Question

  • Hi friends.

      let me tell you first that i m very new at Entity framwork. Today, i am playing with .edmx and repository pattern. I have created and ProfileModel.edmx which contains only one profile table or entity. and it contains 23 fields and one name field is not null out of all 23 fields. 

    When I see in designer.cs fille, It has createProfile method with only two parameters, one is id and second is name. so what about all other fields which I need to set in create method. and I got compilation error.

     

     

     

    Thank you,

    Hiren Visavadiya


    Hiren Visavadiya C#.net Developer
    Thursday, December 22, 2011 6:08 AM

Answers

  • On 12/22/2011 1:08 AM, Hiren Visavadiya wrote:
    > Hi friends.
    >
    > let me tell you first that i m very new at Entity framwork. Today, i am
    > playing with .edmx and repository pattern. I have created and
    > ProfileModel.edmx which contains only one profile table or entity. and
    > it contains 23 fields and one name field is not null out of all 23 fields.
    >
    > When I see in designer.cs fille, It has createProfile method with only
    > two parameters, one is id and second is name. so what about all other
    > fields which I need to set in create method. and I got compilation error.
    >
    It's looking at the entity as a whole as a public partial class, and
    within class the createmethod() for the entity it's creating the entity,
    settings the entity key and returning a new entity.
     
    And then after that,  you see all the public properties that can be
    set/get within the entity.
     
    The public properties are there if you look at an entity in the designer
    those same properties you are seeing in the pane of the graphical
    display of the entity.
     
    You create the entity object new in your code that is not part of the
    designer.
     
    var obj = new someentity();
     
    You address the public properties of the entity from that point of
    either you set the property with data or you get data from the property
    of the object,
     obj.Name = "Mike";  // set
     
    string name = obj.Name; // get
     
    All the code in addressing the entity is in that designer.cs including
    the methods/behavior of the entity/object.
     You don't address the entity's key property when doing a create as EF
    will populate the entity/object itself with the returned key if it's
    successfully saved.
     You set the entity's key  property when you want to get, update or
    delete an existing entity.
     
    It's upto you in code to deal with the entity/object properties of
    getting data in the object or setting data  in the object based on the
    object's public property doing it in code outside of the designer.cs,
    like dealing with the entities in a (DAL) Data Access Layer classlib
    project that has reference to the EF Model.
     
     
    Just follow the project in the link, and there is one for VB too.
     
    • Proposed as answer by Allen_MSDN Friday, December 23, 2011 5:15 AM
    • Marked as answer by Allen_MSDN Monday, December 26, 2011 1:35 AM
    Thursday, December 22, 2011 6:50 AM

All replies

  • On 12/22/2011 1:08 AM, Hiren Visavadiya wrote:
    > Hi friends.
    >
    > let me tell you first that i m very new at Entity framwork. Today, i am
    > playing with .edmx and repository pattern. I have created and
    > ProfileModel.edmx which contains only one profile table or entity. and
    > it contains 23 fields and one name field is not null out of all 23 fields.
    >
    > When I see in designer.cs fille, It has createProfile method with only
    > two parameters, one is id and second is name. so what about all other
    > fields which I need to set in create method. and I got compilation error.
    >
    It's looking at the entity as a whole as a public partial class, and
    within class the createmethod() for the entity it's creating the entity,
    settings the entity key and returning a new entity.
     
    And then after that,  you see all the public properties that can be
    set/get within the entity.
     
    The public properties are there if you look at an entity in the designer
    those same properties you are seeing in the pane of the graphical
    display of the entity.
     
    You create the entity object new in your code that is not part of the
    designer.
     
    var obj = new someentity();
     
    You address the public properties of the entity from that point of
    either you set the property with data or you get data from the property
    of the object,
     obj.Name = "Mike";  // set
     
    string name = obj.Name; // get
     
    All the code in addressing the entity is in that designer.cs including
    the methods/behavior of the entity/object.
     You don't address the entity's key property when doing a create as EF
    will populate the entity/object itself with the returned key if it's
    successfully saved.
     You set the entity's key  property when you want to get, update or
    delete an existing entity.
     
    It's upto you in code to deal with the entity/object properties of
    getting data in the object or setting data  in the object based on the
    object's public property doing it in code outside of the designer.cs,
    like dealing with the entities in a (DAL) Data Access Layer classlib
    project that has reference to the EF Model.
     
     
    Just follow the project in the link, and there is one for VB too.
     
    • Proposed as answer by Allen_MSDN Friday, December 23, 2011 5:15 AM
    • Marked as answer by Allen_MSDN Monday, December 26, 2011 1:35 AM
    Thursday, December 22, 2011 6:50 AM
  • Hey friend,

    Thank you for this information. 


    Hiren Visavadiya C#.net Developer
    Thursday, December 22, 2011 7:01 AM
  • thank for information
    Tuesday, March 4, 2014 6:32 PM