I have created .edmx file and have generated model classes from that. Whenever I do some changes in db and update the .edmx file via Update Model it overwrites my existing model classes I am loosing all addition creted properties and DataAnnotations.
Please suggest how can I update my .edmx file without effecting existing model classes.
Thanks for the reply. But I am not getting you clearly.
public partial class PRODUCT
{
public int id { get; set; }
public string Name{ get; set; }
public string SKU { get; set; }
}
This is generated model and I changed following
[DisplayName("Name")]
[Required(ErrorMessage = "Please enter Name.")]
[MaxLength(255,ErrorMessage="Please enter valid Name.")]
public string Name{ get; set; }
etc....
Now I added new fields in product table in db and update the .edmx file via Update Model from database and it overwrite my whole model class and I lost all additional property and data annotation I made.
You cannot update the partial classes generated by the edmx model. You need to create a separate class file and create partial public classes with the same name of the model classes generated. The link explains that. When you update your edmx model it
will not overwrite your manually created partial public classes.
kyuti
Contributor
2027 Points
549 Posts
Update .edmx file without effecting currently generated model
Feb 27, 2013 09:27 AM|LINK
Hello All,
I have created .edmx file and have generated model classes from that. Whenever I do some changes in db and update the .edmx file via Update Model it overwrites my existing model classes I am loosing all addition creted properties and DataAnnotations.
Please suggest how can I update my .edmx file without effecting existing model classes.
Thank you.
Kalpana Patel
DarrellNorto...
All-Star
86703 Points
9638 Posts
Moderator
MVP
Re: Update .edmx file without effecting currently generated model
Feb 27, 2013 09:34 AM|LINK
Don't change the model classes that are created. They are overwritten when you update your EDMX.
EF generates partial classes by default. So you can put your logic in a "buddy class" that does not get overwritten.
See this blog post for details: http://robbincremers.me/2012/01/31/entity-framework-using-partial-classes-to-add-business-logic-and-validation-to-generated-entities/
Darrell Norton's Blog
Please click "Mark as Answer" if this helped you.
kyuti
Contributor
2027 Points
549 Posts
Re: Update .edmx file without effecting currently generated model
Feb 27, 2013 09:46 AM|LINK
Thanks for the reply. But I am not getting you clearly.
public partial class PRODUCT
{
public int id { get; set; }
public string Name{ get; set; }
public string SKU { get; set; }
}
This is generated model and I changed following
[DisplayName("Name")]
[Required(ErrorMessage = "Please enter Name.")]
[MaxLength(255,ErrorMessage="Please enter valid Name.")]
public string Name{ get; set; }
etc....
Now I added new fields in product table in db and update the .edmx file via Update Model from database and it overwrite my whole model class and I lost all additional property and data annotation I made.
Please suggest how to avoid it.
Kalpana Patel
kyuti
Contributor
2027 Points
549 Posts
Re: Update .edmx file without effecting currently generated model
Feb 27, 2013 11:00 AM|LINK
Any help?
Kalpana Patel
mitzman
Member
7 Points
4 Posts
Re: Update .edmx file without effecting currently generated model
Feb 28, 2013 04:57 PM|LINK
You cannot update the partial classes generated by the edmx model. You need to create a separate class file and create partial public classes with the same name of the model classes generated. The link explains that. When you update your edmx model it will not overwrite your manually created partial public classes.