Entity Framework Database First with proper use of Data Anotations

时光怂恿深爱的人放手 提交于 2019-12-11 06:19:45

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/16101091/entity-framework-database-first-with-proper-use-of-data-anotations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!