Jacek Gorgoń
Jacek Gorgoń

Reputation: 3214

Ignore related entity's property

I'm using EF 4.3.1 code first to load data from entity Users

public class User
{
    public int Id { get; set; }
    public IList<UserFile> Files { get; set; }
}

along with the Files

public class UserFile
{
    public int Id { get; set; }
    public User User { get; set; }
    public string Name { get; set; }
    public byte[] Data { get; set; }
}

using code

var users = Context.Users.Include(u => u.Files).ToList();

Now this works, but the files are potentially quite large. What I'd like to do is load everything except the actual Data property, so that a long list of users with just their file names can be efficiently displayed. Is there some clean way to approach this?

My best idea so far is introducing another entity File related 1-1 to UserFile and containing only the Data field, with Name remaining in UserFile. I'd rather not add another redundant table (from DB PoV) just to make loading easier for EF...

Upvotes: 1

Views: 220

Answers (1)

StriplingWarrior
StriplingWarrior

Reputation: 156634

One approach would be to create a UserBasicFile entity that includes everything but the data, and then make UserFile extend UserBasicFile, and include the Data property. That way if you have utility functions that only require the basic information they could require UserBasicFiles, and UserFiles could still be provided to those functions.

Remember that you don't necessarily have to have two tables in order to have two entity types.

Upvotes: 2

Related Questions