如何从延迟加载和单表继承的数据库获取数据 [英] How to get data from database with lazy loading and single table inheritance

查看:227
本文介绍了如何从延迟加载和单表继承的数据库获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库,设置了延迟加载打开(这是必需的)。
我也有一个ASP.NET MVC应用程序连接到数据库和'Does Stuff'。
数据库是在单独的项目中从我的类建模的。

I have a database that is set up with lazy loading turned on(which is needed). I also have an ASP.NET MVC application that connects to the database and 'Does Stuff'. The database is modeled from my classes in a separate project.

我目前有3个类使用继承:

I currently have 3 classes that use inheritance:

public class DeliveryNotification
    {
        [Key]
        public int Id { get; set; }

        public Status Status { get; set; }
    }

    public class TrackedDelivery : DeliveryNotification
{
    [Required]
    public DateTime Date { get; set; }

    [Required]
    public DateTime Time { get; set; }
}

public class SignedForDelivery : TrackedDelivery
    {
        [Required]
        public string Image { get; set; }

    }



这些都存储在数据库中使用单表'DeliveryNotification'

These are all stored in the database using single table called 'DeliveryNotification'

我已获得一个DeliveryNotification ID,并希望获取所有信息。

I have been given a DeliveryNotification Id and would like to get all of the information.

我的控制器之一是这个方法:

Inside one of my controllers is this method:

var query = (from s in context.SignedForDeliverys
                    where s.Id == id
                             select s
                    ).SingleOrDefault();

^虽然有一个ID为
的DeliveryNotification,

^ This will return null although there is a DeliveryNotification with the ID

var deliveryNotifications = context.DeliveryNotifications.SingleOrDefault(o => o.Id == id);

^这将只返回id和状态。

^ This will return only the id and the status.

因此,假设我已经获得了一个Id,我如何返回(最好是一个SignedForDeliverys)所有的数据?

So assuming I have been given an Id how can I return (preferably a SignedForDeliverys) with all of the data?

编辑:
尝试添加包含:

var signedForDeliverys = (from s in context.SignedForDeliverys.Include("TrackedDelivery ").Include("DeliveryNotification")
                select s).ToList();

然后我得到这个错误:

An exception of type 'System.InvalidOperationException' occurred in EntityFramework.SqlServer.dll but was not handled in user code

Additional information: A specified Include path is not valid. The EntityType 'ClassLibrary.SignedForDelivery' does not declare a navigation property with the name 'TrackedDelivery'.


推荐答案

查询1:

var query = (from s in context.SignedForDeliverys
            where s.Id == id
            select s
            ).SingleOrDefault();

应返回 SignedForDelivery a SignedForDelivery 。如果id匹配 DeliveryNotification ,而不是 SignedForDelivery ,则它将返回null。

should return a SignedForDelivery if the id matches a SignedForDelivery. If the id matches a DeliveryNotification that is not a SignedForDelivery then it will return null.

查询2:

var deliveryNotifications = context.DeliveryNotifications
                                   .SingleOrDefault(o => o.Id == id);

应返回 DeliveryNotification a DeliveryNotification 。要将其转换为 TrackedDelivery SignedForDelivery ,您需要投射:

should return a DeliveryNotification if the id matches a DeliveryNotification. To get it as TrackedDelivery or a SignedForDelivery you need to cast it:

var trackedDelivery = deliveryNotifications as DeliveryNotification;
if(trackedDelivery != null)
{
   DateTime d = trackedDelivery.Date;
}

var signedForDelivery = deliveryNotifications as SignedForDelivery 
if(signedForDelivery != null)
{
   String image = signedForDelivery.Image;
}

查询3:

var signedForDeliverys = (from s in context.SignedForDeliverys
                                           .Include("TrackedDelivery")
                                           .Include("DeliveryNotification")
                          select s).ToList();

失败,因为Include方法的目的是加载相关实体。即通过导航属性相关的实体。 SignedForDelivery 不会声明名为 TrackedDelivery 的导航属性

fails because the purpose of the Include method is to load related entities. i.e entities that are related via navigation properties. SignedForDelivery does not declare a navigation property with the name TrackedDelivery

这篇关于如何从延迟加载和单表继承的数据库获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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