0

I use ASP.NET MVC 5 and Entity Framework 6. So I have generated some files under *.tt

I would like to keep attributes that i inserted into those files because I have created many pages automatically based on Entity Framework classes.

But when I update Entity Framework model from the database I loose everything that I inserted.

So my question is how to keep it from deleting?

//------------------------------------------------------------------------------ // // This code was generated from a template. // // Manual changes to this file may cause unexpected behavior in your application. // Manual changes to this file will be overwritten if the code is regenerated. // //------------------------------------------------------------------------------

namespace MyWebSIte.DataModel
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;

    public partial class ItemMovement
    {
        public System.Guid ID { get; set; }

        [DataType(DataType.DateTime)]  <---- I would like to keep it.
        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
        [Display(Name = "Fecha")]
        public System.DateTime? Changed { get; set; }
NoWar
  • 36,338
  • 80
  • 323
  • 498

3 Answers3

2

Those files are not meant to be edited manually. You should take advantage of the fact that those classes are partial and can be "extended". Take a look at Metadata classes, they will allow you to annotate properties.

kiziu
  • 1,111
  • 1
  • 11
  • 15
  • It is for RIA Services. I dont use it. Are you sure we can use it for ASP.NET MVC? – NoWar Sep 14 '16 at 15:44
  • 1
    Frankly, I only focused on the part which shows how to use Metadata classes. The principal is the same - you create a metadata class, you create a partial class for entity type and mark it with attribute. Take a look here: http://stackoverflow.com/a/4916085/6804888 – kiziu Sep 14 '16 at 15:48
  • 1
    Of course there is a second option which involves creating `interface` for the entity and marking the properties in it with annotations, but it is also described in the SO link I provided. – kiziu Sep 14 '16 at 15:51
2

The generated class ItemMovement is a partial class.
This allows you to write a second partial class which is marked with the necessary data annotations.
In your case the partial class ItemMovement would look like this:

   namespace MyWebSIte.DataModel
    {
        using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.ComponentModel.DataAnnotations;

        [MetadataType(typeof(ItemMovementMetaData))]
        public partial class ItemMovement
        {
            public System.Guid ID { get; set; }        
            public System.DateTime? Changed { get; set; }
        }

        public partial class ItemMovementMetaData {

            [DataType(DataType.DateTime)]
            [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
            [Display(Name = "Fecha")]
            public System.DateTime? Changed { get; set; }   

            //....................
        }
    }
  • Could we generate it automatically in ASP.NET MVC? – NoWar Sep 14 '16 at 15:57
  • How would you have them generated automatically, if you have to provide the annotations manually? – kiziu Sep 14 '16 at 15:59
  • You can't generate them dynamically you have to write them manually – user3764415 Sep 14 '16 at 16:05
  • I mean all entity classes under *.tt generated by Entity Framework but I am asking if it possible to generate MetadataCalsses the same way like we do it in RIA Services. – NoWar Sep 14 '16 at 16:06
  • Your answer is correct but I found a similar one http://stackoverflow.com/questions/2229716/auto-generating-metadata-classes-for-entity-framework and over there I also found this app http://pfsolutions-mi.com/Product/MetaDataClassGenerator that does all job I need to do manually. – NoWar Sep 14 '16 at 16:58
  • Is this still up to date? – FllnAngl May 24 '18 at 14:07
0

You can use the codefirst which gives u max control over your models and dbcontext.

Step1 : Create a codefirst model using the existing database. codefirst model using the existing database

Step 2: now type enable-migrations in the package manager console

Step 3: You use the (db)context and the model generated from the database table. make changes to your model

Step 4: type add-migration [some name to identify the migration] in the package manager console

Step 5: check the generated migration file.

Step 6: type update-database in the package manager console

Now your changes are updated to the database. From now on you can us the codefirst approach to handle the changes to your database.

srini
  • 170
  • 4