Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

loosing dataAnottation when upload model from database

I have a big database existing database to comunicate with, and I'm using EF 5.0 database first, the problem I'm having is that if I create any data decoration like [stringlength(50)] on the class and then the databases is uploaded, when I "upload from database" all data annotations are gone. How can I do to keep them?

like image 768
EricGS Avatar asked Jul 19 '13 11:07

EricGS


People also ask

Why is it important to minimize the amount of data loaded?

Despite the efficiencies achieved by the VertiPaq storage engine, it is important that you strive to minimize the data that is to be loaded into your models. It is especially true for large models, or models that you anticipate will grow to become large over time. Four compelling reasons include:

Which data annotation attributes have been applied to the postedfile property?

The following Model class consists of one property PostedFile to which the following validation Data Annotation attributes have been applied. 1. Required Data Annotation attribute. 2. RegularExpression Data Annotation attribute. The RegularExpression Data Annotation attribute accepts the Regular Expression as first parameter.

How can I avoid loading a query to the model?

To avoid loading the query to the model, take care to ensure that you disable query load in these instances. Power BI Desktop includes an option called Auto date/time.

How to reduce the size of data in import modeling?

Data reduction techniques for Import modeling 1 Remove unnecessary columns. ... 2 Remove unnecessary rows. ... 3 Group by and summarize. ... 4 Optimize column data types. ... 5 Preference for custom columns. ... 6 Disable Power Query query load ... 7 Disable auto date/time. ... 8 Switch to Mixed mode. ... 9 Next steps


1 Answers

It's very simple: You Can't! Because those codes are auto-generated and will be over written on each model update or change.

However you can achieve what you need through extending models. Suppose that EF generated the following entity class for you:

namespace YourSolution
{
    using System;
    using System.Collections.Generic;

    public partial class News
    {
        public int ID { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }        
        public int UserID { get; set; }

        public virtual UserProfile User{ get; set; }
    }
}

and you want do some work arounds to preserve your you data annotations and attributes. So, follow these steps:

First, add two classes some where (wherever you want, but it's better to be in Models) like the following:

namespace YourSolution
{
    [MetadataType(typeof(NewsAttribs))]
    public partial class News
    {
         // leave it empty.
    }

    public class NewsAttribs
    {            
        // Your attribs will come here.
    }
}

then add what properties and attributes you want to the second class - NewsAttribs here. :

public class NewsAttrib
{
    [Display(Name = "News title")]
    [Required(ErrorMessage = "Please enter the news title.")]
    public string Title { get; set; }

    // and other properties you want...
}

Notes:

1) The namespace of the generated entity class and your classes must be the same - here YourSolution.

2) your first class must be partial and its name must be the same as EF generated class.

Go through this and your attribs never been lost again ...

like image 128
Amin Saqi Avatar answered Sep 18 '22 12:09

Amin Saqi