Entity framework 虚拟导航 属性

Entity framework virtual navigation property

我使用 EF6 开发 Web 应用程序。

假设我有以下型号:

public interface IBaseEntityObject 
{
    public int Id {get; set;}
}


public abstract class BaseEntityObject : IBaseEntityObject
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id {get; set;}
}


public class Folder : BaseEntityObject
{   
    public string Name {get; set;}

    public List<Letter> Letters {get; set;} 
}


public abstract class Letter : BaseEntityObject
{   
    public string Title {get; set;}

    public string Content {get; set;}

    public virtual Folder Folder {get; set;}

    public int FolderId {get; set;}

    public DateTime CreationDate {get; set;}
}

public class OutgoingLetter : Letter
{
    // .. OutgoingLetter properties
}

public class ReceviedLetter : Letter
{
    // .. ReceviedLetter properties
}

public class MyDbContext : DbContext
{
    public DbSet<Folder> Folders {get; set;}

    public DbSet<Letter> Letters {get; set;}


    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        // Folder <-> Letters       
        modelBuilder.Entity<Letter>()
        .HasRequired(t => t.Folder)
        .WithMany(f => f.Letters)
        .HasForeignKey(t => t.FolderId)
        .WillCascadeOnDelete(true);
    }
}

如果我从 Letter 模型中删除虚拟文件夹导航 属性 会有任何损失吗?我不希望我的客户在要求一封信时收到一个文件夹……这似乎是错误的。

我只是想知道如果删除这个 属性,我是否会失去一些 EF 性能。

谢谢。

I don't want my clients to receive a folder when they ask for a letter..seems wrong.

您不应该 return 向您的客户发送域对象,而应该 数据传输对象 Check Martin Folwer's definition:

When you're working with a remote interface, such as Remote Facade (388), each call to it is expensive. As a result you need to reduce the number of calls, and that means that you need to transfer more data with each call. One way to do this is to use lots of parameters. However, this is often awkward to program - indeed, it's often impossible with languages such as Java that return only a single value.

The solution is to create a Data Transfer Object that can hold all the data for the call. It needs to be serializable to go across the connection. Usually an assembler is used on the server side to transfer data between the DTO and any domain objects.