Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 映射实体并使用父级的新属性从实体框架的实体继承_C#_Entity Framework_Oop - Fatal编程技术网

C# 映射实体并使用父级的新属性从实体框架的实体继承

C# 映射实体并使用父级的新属性从实体框架的实体继承,c#,entity-framework,oop,C#,Entity Framework,Oop,我正在使用实体框架。我的.net Framework是3.5,由于公司的政策,我无法改进它。我的模型是数据库优先,我有一个名为RequestMaster的实体。我不想直接使用它,然后我为继承它的每个实体创建了一个派生类 现在假设我的RequestMaster是: public class RequestMaster { public int RequestId{ get; set; } public RequestType RequestType { g

我正在使用实体框架。我的.net Framework是3.5,由于公司的政策,我无法改进它。我的模型是数据库优先,我有一个名为RequestMaster的实体。我不想直接使用它,然后我为继承它的每个实体创建了一个派生类

现在假设我的RequestMaster是:

public class RequestMaster
    {
        public int RequestId{ get; set; }

        public RequestType RequestType { get; set; }
    }
派生类是:

public class Request : RequestMaster
{

    public List<Request> SelectAll()
    {

        return _dataContext.RequestMaster
                        .Include("RequestType")
                        .Select(x => new Request()
                        {

                            RequestId = x.RequestId,
                            RequestType = x.RequestType,


                        }).ToList();
    }

}
我的问题是RequestId和RequestType的所有属性都是零或空的,我已经检查过了,一切正常,但问题在于RequestMaster到Request的映射。我更改了请求并覆盖了RequestMaster的属性,问题就解决了

 public class Request : RequestMaster
    {


        public new int  RequestId { get; set; }
        public new int RequestType { get; set; }

        public void SelectAll()
        {

            return _dataContext.RequestMaster
                            .Include("RequestType")
                            .Select(x => new Request()
                            {

                                RequestId = x.RequestId,
                                RequestType = x.RequestType,


                            }).ToList();
        }

    }

有谁能告诉我映射出了什么问题以及新属性是如何解决的吗?

您不需要继承:

要根据请求塑造结果,甚至不需要继承,只需塑造它即可。 要将方法添加到RequestMaster,您不需要继承,只需装入另一个文件并创建一个公共部分类RequestMaster,然后将您的方法public List SelectAll或public List SelectAll添加到其中。 要从基本实体驱动类并使映射仍然有效,您应该使用EF中的标准继承机制,例如。 但总的来说,我强烈建议不要这样做,而是分开关注点,使用另一个名为RequestMasterBusinessLogic的类来包含该逻辑, 您还可以在此类中塑造您的实体,如果您确实也需要一个请求类,请在RequestMasterBusinessLogic中编写此代码,而无需从RequestMaster派生:


为什么SelectAll方法的返回类型为void?SelectAll方法不应该有返回类型吗?不是void?对不起,它的返回类型是List,我的错误是在问题中更改了它。我编辑了这个问题。我几乎可以肯定你根本不需要选择,除非我遗漏了什么。您只需编写:return\u dataContext.RequestMaster.IncludeRequestType.ToList;。我不知道为什么你的代码不起作用,但通常不建议在通过EF进行查询时手动创建实体。实际上没有什么意义-最好返回一个viewmodel,或者另一个与数据库无关的对象。为什么你需要从RequestMaster继承请求?你不能用合成来代替吗?是的,它很有用,我会用它,谢谢,但是你能告诉我我的映射有什么问题吗?为什么要使用新属性?感谢您的反馈@samirariazati,说实话,我不确定在使用new时使用解决方案的原因,但我知道当您需要使用EF继承时,您还应该编辑映射。如果我对您的评论有更好的回答,我会通知您:
 public class Request : RequestMaster
    {


        public new int  RequestId { get; set; }
        public new int RequestType { get; set; }

        public void SelectAll()
        {

            return _dataContext.RequestMaster
                            .Include("RequestType")
                            .Select(x => new Request()
                            {

                                RequestId = x.RequestId,
                                RequestType = x.RequestType,


                            }).ToList();
        }

    }
public partial class RequestMasterBusinessLogic
{
    private YourDataContext Context;
    public RequestMasterBusinessLogic()
    {
        Context= new YourDataContext();
    }
    public List<RequestMaster> SelectAll()
    {
         Context.RequestMaster.Include("RequestType").ToList();
    }
    public List<Request> SelectAllRequests()
    {
         Context.RequestMaster
                .Include("RequestType")
                .Select(x => new Request()
                {
                    RequestId = x.RequestId,
                    RequestType = x.RequestType,
                }).ToList();
    }
}