实体框架数据库优先,正确使用数据注释 [英] Entity Framework Database First with proper use of Data Anotations

查看:111
本文介绍了实体框架数据库优先,正确使用数据注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 EF 和Code First方法的项目,因此使用数据注释很简单。现在,我正在使用数据库优先,并且我发现使用数据注释更具体,因此我想知道实现它的正确步骤。

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:

ModelExtensions 中是我创建的所有文件,用于将数据注释添加到 DbContextModel.tt 实体。

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

这是 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; }
    }
}

我在这里有几个问题。首先-名称空间。是这样的名称空间DataAccess.ModelExtensions 还是我必须删除 .ModelExtensions 部分。我最初在看一个使用DB的项目,那里的命名空间只是 DataAccess 不知道为什么需要它(如果需要)。另外-我是否需要向 DbContextModel.tt 实体添加一些其他引用?现在,我为此使用标准C#类,然后将其重命名为:公共部分类MCS_ContentTypes:BaseEntity 。我是否必须使用一种特殊的方法来创建那些对象,以显式公开实体与该文件之间的连接?

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)扩展模型的名称空间必须与EF自动生成的实体类的名称空间相同-如果 DbContextModel.tt 实体类的名称空间为 DataAccess ,则应将类的命名空间设置为 DataAccess

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)我没有完全解决您的问题,但是采用这种方法,实体类的名称和您的类必须相同。

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

下面的示例说明了它应该是什么。假设EF为您生成了以下实体类:

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...
    }
}

因此,您不需要任何:BaseEntity 继承。

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

这篇关于实体框架数据库优先,正确使用数据注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆