Entity Framework Database First with proper use of Data Anotations


I have one project with EF and Code First approach and there using of Data Annotations was straight forward. Now I'm working with Database First and I see that using Data Annotations is more specific so I want to know the right steps to implement it.

The structure of my project that provides Data Access is this:

<img alt="DataAccess" class="b-lazy" data-src="https://i.stack.imgur.com/52jgd.png" data-original="https://i.stack.imgur.com/52jgd.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

In ModelExtensions are all my files that I've created to add the Data Annotations to the DbContextModel.tt entities.

Here is the structure of one of my files in ModelExtensions:

using System; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; namespace DataAccess.ModelExtensions { [MetadataType(typeof(MCS_ContentTypesMetaData))] public partial class MCS_ContentTypes : BaseEntity { } internal sealed class MCS_ContentTypesMetaData { [Required] [StringLength(10)] public string Name { get; set; } } }

I have several questions here. First - the namespace. Should it be like this namespace DataAccess.ModelExtensions or I have to remove the .ModelExtensions part. I was looking at a project using DB first and there the namespace was just DataAccess not sure why it is needed (if so). Also - Do I need to add some other references to the DbContextModel.tt entities? Now I use standard C# classes for this and then rename them to : public partial class MCS_ContentTypes : BaseEntity. Do I have to use a special approach for creating those to explicitly expose the connection between the entity and this file?


1) The namespace of your extension models must be the same as the namespace of EF auto-generated entity classes - If the namespace of DbContextModel.tt entity classes is DataAccess, you should set the namespace of your classes to DataAccess.

2) I doesn't get your question completely, however in this approach, names of entity classes and your classes must be the same.

The following example shows what it should be. Suppose that EF generates 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; } } }

So, your partial classes should be like the following:

namespace YourSolution { [MetadataType(typeof(NewsAttribs))] public partial class News { // leave it empty. } public class NewsAttribs { // Your attribs will come here. [Display(Name = "News title")] [Required(ErrorMessage = "Please enter the news title.")] public string Title { get; set; } // and other properties you want... } }

So, you doesn't need any : BaseEntity inheritance.


  • Entity Framework Database First with proper use of Data Anotations
  • There is no store type corresponding to the conceptual side type 'Geography' of primitive
  • Microsoft Bot : How to capture Too Many Attempts in Form Flow?
  • MetadataType buddy class for EF generated POCO - attributes are ignored by XmlSerializer
  • ASP.Net MVC2 - Set ViewModel values with retrieved from db object's values with DataAnnotations
  • MVC 3 passing entity as an Interface
  • Localizing MVC3 Validation Messages
  • How do I make an abstract JPA entity that has an ordered column in a shared object?
  • ValidateInputAttribute does not contain a definition for Exclude
  • How to create custom validation attribute?
  • Model layer dependency on MVC attributes
  • How can I use database Views in a scaffolded DbContext with Entity Framework Core 1.0 (EF7)
  • EF self one-to-many relationship
  • ASP.NET MVC 2 - Property validation
  • Getting values from mysql database to show in JSP (JAVA)
  • Client side validation not working with reused and nested complex property
  • How can I get NHibernate to map to a Tuple or Class?
  • Oracle data access version issue
  • Class to DataSet / DataSet to class
  • Whats the best approach to use q promise in a loop? Waiting for chain to complete before iterating t
  • Data Type of Columns in a List - R
  • C# DynamicLinq where clause with Any()
  • How to write string.Contains(someText) in expression Tree
  • make EF map byte array to binary instead of varbinary
  • How to replace TouchesBegan with UIGestureRecognizer
  • DependencyObject.AssociatedObject is always null
  • when does setTimeout start executing in a inline
  • How do I register classes by both interface and namespace with Windsor?
  • Cannot Parse HTML Data Using Android / JSOUP
  • JTable with a ScrollPane misbehaving
  • unknown Exception android
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?