Update model from database (database first)

I am using MVC3 VS2010 with EF4.1, I created my database using SQL Server and I am importing it into the MVC3 web application.

I have a problem here when I come to update the model from the database. I have lost all modifications of my models, for example, if I use attributes in some models for verification, or, thus, everything that is overwritten with new model properties.

Is there a way to update a model from a database without losing model information?

OR

where should I define validation on my models and not directly use model files?

+6
source share
4 answers

Update:. Since this is still relatively popular, I created a blog post.

http://jnye.co/Posts/19/adding-validation-to-models-created-by-entity-framework-database-first-c

If you want to test your models and not use viewModels, use partial classes to define validation attributes. For instance:

Say you have a model like

public class User { public string Name { get; set; } } 

If you want to put a string length verifier on it, you will need to create an incomplete class and use MetadataTypeAttribute (this lives in System.ComponentModel.DataAnnotations)

The following classes must be defined in a separate NOT file in the same file as your automatically generated models.

 [MetadataTypeAttribute(typeof(UserMetadata))] public partial class User { } 

Then you define your validation in the UserMetadata class as follows

 public class UserMetadata{ [StringLength(50)] public string Name {get; set;} } 

EDIT

I just found this article that explains this solution in more detail http://themonitoringguy.com/tips-tricks/validating-microsoft-entity-framework-objects-c-mvc/

+11
source

No, files will be restored every time.

All classes are defined as partial, so you can easily add DataAnnotations using MetadataTypeAttribute .

Say you have a User class, which is defined as follows:

 public partial class User { public string Name {get;set;} } 

Create an IUser Interface

 public interface IUser { [Required] [DisplayName("User name")] string Name {get;set;} } 

And then extend the User class to indicate that IUser will be used as metadata.

 [MetadataType(typeof(IUser))] public partial class User {} //Empty class body 
+3
source

The first rule of any constructor: it generates any code that you cannot change, because it will be completely deleted the next time you update anything in the constructor.

All created classes are partial, so you can create your own partial part and place your own logic. Obviously, you cannot add attributes to properties defined in the automatically generated part. In the case of data annotations, you can either through buddy classes or a custom T4 template that will contain your own logic to decide which data annotations should be added during code generation. Both scenarios are in most cases considered bad practice because you must have a separate presentation model for each view with the validation needed specifically for that view.

+1
source

Make sure that the MainClass namespace is the same as Partial and has the same attributes. This is my decision.

example:

Metadata: Create It Anywhere You Want

 public class FormMetadata { public int Id { get; set; } public string Description { get; set; } public Nullable<bool> IsEnable { get; set; } public Nullable<System.DateTime> CreationDate { get; set; } public int CompanieId { get; set; } public string RegularExpression { get; set; } public virtual ICollection<Field> Fields { get; set; } [JsonIgnore] public virtual Company Company { get; set; } } 

Mainclass

 namespace Transactions.Model { public partial class Form { [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")] public Form() { this.Fields = new HashSet<Field>(); } public int Id { get; set; } public string Description { get; set; } public Nullable<bool> IsEnable { get; set; } public Nullable<System.DateTime> CreationDate { get; set; } public int CompanieId { get; set; } public string RegularExpression { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<Field> Fields { get; set; } public virtual Company Company { get; set; } } } 

Partial use of MetadataType

 namespace Transactions.Model { [MetadataTypeAttribute(typeof(FormMetadata))] public partial class Form { } } 

Should you have problems creating a partial class in the same namespace? Do not worry

  1. Create a folder
  2. Create a partial class in this folder
  3. Change the namespace to the same from MainClass
0
source

Source: https://habr.com/ru/post/893127/


All Articles