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?
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:
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.
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.
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
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 ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With