Answered by:
Create Method in Entity Framwork

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 DeveloperThursday, 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, andwithin 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 beset/get within the entity.The public properties are there if you look at an entity in the designerthose same properties you are seeing in the pane of the graphicaldisplay of the entity.You create the entity object new in your code that is not part of thedesigner.var obj = new someentity();You address the public properties of the entity from that point ofeither you set the property with data or you get data from the propertyof the object,obj.Name = "Mike"; // setstring name = obj.Name; // getAll the code in addressing the entity is in that designer.cs includingthe methods/behavior of the entity/object.You don't address the entity's key property when doing a create as EFwill populate the entity/object itself with the returned key if it'ssuccessfully saved.You set the entity's key property when you want to get, update ordelete an existing entity.It's upto you in code to deal with the entity/object properties ofgetting data in the object or setting data in the object based on theobject's public property doing it in code outside of the designer.cs,like dealing with the entities in a (DAL) Data Access Layer classlibproject 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, andwithin 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 beset/get within the entity.The public properties are there if you look at an entity in the designerthose same properties you are seeing in the pane of the graphicaldisplay of the entity.You create the entity object new in your code that is not part of thedesigner.var obj = new someentity();You address the public properties of the entity from that point ofeither you set the property with data or you get data from the propertyof the object,obj.Name = "Mike"; // setstring name = obj.Name; // getAll the code in addressing the entity is in that designer.cs includingthe methods/behavior of the entity/object.You don't address the entity's key property when doing a create as EFwill populate the entity/object itself with the returned key if it'ssuccessfully saved.You set the entity's key property when you want to get, update ordelete an existing entity.It's upto you in code to deal with the entity/object properties ofgetting data in the object or setting data in the object based on theobject's public property doing it in code outside of the designer.cs,like dealing with the entities in a (DAL) Data Access Layer classlibproject 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 DeveloperThursday, December 22, 2011 7:01 AM -
thank for informationTuesday, March 4, 2014 6:32 PM