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:
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?