我可以使用ADO.Net实体框架延迟加载标量属性吗?

时间:2022-06-07 02:10:45

I've got a database table Image with one huge column: Data.

我有一个数据库表Image有一个巨大的列:数据。

我可以使用ADO.Net实体框架延迟加载标量属性吗?

I'd rather lazy load that one column so I'm not querying all that when I get a list of all my images.

我宁愿懒得加载一列,所以当我得到所有图像的列表时,我不会查询所有这些。

I know I can put the data into its own column and entity like this:

我知道我可以将数据放入自己的列和实体中,如下所示:

我可以使用ADO.Net实体框架延迟加载标量属性吗?

But, do I have to?

但是,我必须这样做吗?

2 个解决方案

#1


Yes I believe you do have to. I don't think it's possible with EF.

是的,我相信你必须这样做。我觉得EF不可能。

You could make an explicit query for the columns you need, and then later for the data-column. Like the suggestion in this post:

您可以对所需的列进行显式查询,然后对数据列进行显式查询。喜欢这篇文章中的建议:

How to load varbinary(max) fields only when necessary with ADO.NET Entity Framework?

如何在必要时使用ADO.NET Entity Framework加载varbinary(max)字段?

But it seems like Linq To SQL provides the possibility, so I'll try to look into if it's coming to EF too.

但似乎Linq To SQL提供了这种可能性,所以我将试着研究它是否也会出现在EF中。

#2


Do you have access to the data schema? In Entity Framework you can't have two entities that reference the same table, **at least you couldn't in the past. With that said. You could create a Sql VIEW with the data column.

您是否可以访问数据模式?在实体框架中,您不能有两个引用同一个表的实体,**至少在过去不可能。照这样说。您可以使用数据列创建Sql VIEW。

CREATE VIEW [dbo].[ImageData]
SELECT
    Id,
    Data
FROM Image

Now you can create an Entity Called Image Data that mirrors your schema above.

现在,您可以创建一个实体调用的图像数据,它反映了上面的模式。

If you need to update the view, that's possible as well, you would simply need to create a trigger on the view called INSTEAD OF TRIGGER.

如果你需要更新视图,那么你也可以在名为INSTEAD OF TRIGGER的视图上创建一个触发器。

-- INSERT Trigger
CREATE TRIGGER [dbo].[TR_ImageData_Update] ON [ImageData]
INSTEAD OF INSERT
AS
BEGIN
    UPDATE [Image]
    SET Data = i.Data
    FROM Inserted AS i
END;
GO

This would probably be ideal in most cases. However if you don't have access to the database schema you might not be able to do this. Instead, you would create a default query off your Image Entity.

在大多数情况下,这可能是理想的。但是,如果您无权访问数据库架构,则可能无法执行此操作。相反,您将从图像实体创建默认查询。

public static IQueryable GetImages(this DbContext db)
{
    var query = db.Images.AsQueryable();
    return query.Select(r => new Image() { Id = r.id....});
}

public static IQueryable GetImageData(this DbContext db, int imageId)
{
    var query = db.Images.AsQueryable();
    query = query.Where(x => x.Id == imageId);
    query = query.Select(x => new ImageData() { Id = r.Id, Data = r.Data });
    return query;
}

#1


Yes I believe you do have to. I don't think it's possible with EF.

是的,我相信你必须这样做。我觉得EF不可能。

You could make an explicit query for the columns you need, and then later for the data-column. Like the suggestion in this post:

您可以对所需的列进行显式查询,然后对数据列进行显式查询。喜欢这篇文章中的建议:

How to load varbinary(max) fields only when necessary with ADO.NET Entity Framework?

如何在必要时使用ADO.NET Entity Framework加载varbinary(max)字段?

But it seems like Linq To SQL provides the possibility, so I'll try to look into if it's coming to EF too.

但似乎Linq To SQL提供了这种可能性,所以我将试着研究它是否也会出现在EF中。

#2


Do you have access to the data schema? In Entity Framework you can't have two entities that reference the same table, **at least you couldn't in the past. With that said. You could create a Sql VIEW with the data column.

您是否可以访问数据模式?在实体框架中,您不能有两个引用同一个表的实体,**至少在过去不可能。照这样说。您可以使用数据列创建Sql VIEW。

CREATE VIEW [dbo].[ImageData]
SELECT
    Id,
    Data
FROM Image

Now you can create an Entity Called Image Data that mirrors your schema above.

现在,您可以创建一个实体调用的图像数据,它反映了上面的模式。

If you need to update the view, that's possible as well, you would simply need to create a trigger on the view called INSTEAD OF TRIGGER.

如果你需要更新视图,那么你也可以在名为INSTEAD OF TRIGGER的视图上创建一个触发器。

-- INSERT Trigger
CREATE TRIGGER [dbo].[TR_ImageData_Update] ON [ImageData]
INSTEAD OF INSERT
AS
BEGIN
    UPDATE [Image]
    SET Data = i.Data
    FROM Inserted AS i
END;
GO

This would probably be ideal in most cases. However if you don't have access to the database schema you might not be able to do this. Instead, you would create a default query off your Image Entity.

在大多数情况下,这可能是理想的。但是,如果您无权访问数据库架构,则可能无法执行此操作。相反,您将从图像实体创建默认查询。

public static IQueryable GetImages(this DbContext db)
{
    var query = db.Images.AsQueryable();
    return query.Select(r => new Image() { Id = r.id....});
}

public static IQueryable GetImageData(this DbContext db, int imageId)
{
    var query = db.Images.AsQueryable();
    query = query.Where(x => x.Id == imageId);
    query = query.Select(x => new ImageData() { Id = r.Id, Data = r.Data });
    return query;
}