【问题标题】:Mapping Entity and Inherit from a Entity Framework's Entity with new the parent's properties使用新的父属性映射实体并从实体框架的实体继承
【发布时间】:2015-11-10 11:18:44
【问题描述】:

我正在使用实体框架。我的 .net 框架是 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();
    }

}

然后我用它:

 var req = new Request ();
 _request = req.SelectAll();

我的问题是所有属性(RequestId 和 RequestType)都为零或 null,我检查并看到一切正常,但问题出在 RequestMaster 到 Request 的映射中。我更改了 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();
        }

    }

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

【问题讨论】:

  • 为什么你的SelectAll 方法的返回类型是void
  • 你的方法 SelectAll 不应该有返回类型吗?而不是 void?
  • 对不起,它的返回类型是 List ,我在问题中更改它的错误。我编辑了这个问题。
  • 我几乎可以肯定你根本不需要选择,除非我遗漏了什么。您可以简单地写:return _dataContext.RequestMaster.Include("RequestType").ToList();。我不知道为什么您的代码不起作用的答案,但通常不建议在通过 EF 查询时手动创建实体(这没什么意义) - 最好返回视图模型或与数据库无关的另一个对象而是。
  • 为什么需要Request继承RequestMaster?你不能用合成来代替吗?

标签: c# entity-framework oop


【解决方案1】:

你不需要继承:

  • 要将结果整形为 Request ,您甚至不需要继承,只需整形即可。
  • 要向RequestMaster 添加方法,您不需要继承,只需创建另一个文件并创建一个public partial class RequestMaster 并将您的方法public List&lt;RequestMaster&gt; SelectAll()public List&lt;Request&gt; SelectAll() 添加到其中。
  • 要从基础实体驱动类并让映射仍然有效,您应该使用 EF 中的标准继承机制,例如 Table Per Type

但总的来说,我强烈建议不要这样做,而是将关注点分开并使用名为 RequestMasterBusinessLogic 的不同类来包含该逻辑, 你也可以在这个类中塑造你的实体,如果你真的也需要一个 Request 类,请在RequestMasterBusinessLogic 中编写此代码,而不是从RequestMaster 派生:

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();
    }
}

【讨论】:

  • 是的,它很有帮助,我会使用它,谢谢,但你能告诉我我的映射有什么问题吗?为什么它适用于新属性?
  • 感谢您的反馈@samirariazati,老实说,当您使用new 时,我不确定解决方案的工作原因,但我知道当您需要使用 EF 继承时,您应该还编辑映射。如果我对您的评论有更好的答案,我会通知您:)
猜你喜欢
  • 1970-01-01
  • 2011-07-25
  • 1970-01-01
  • 2015-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-17
  • 1970-01-01
相关资源
最近更新 更多