Entity framework DDD EF存储库

Entity framework DDD EF存储库,entity-framework,repository,domain-driven-design,aggregateroot,Entity Framework,Repository,Domain Driven Design,Aggregateroot,使用以下DDD和存储库模式,是否可以返回已包含其子数据的聚合根对象,而不是使用延迟加载 e、 g.我有一个仓库实体作为聚合根,它有一个名为location的子对象 在存储库上,我在下面有一个查询位置Id的方法,但会传回仓库实体 dim warehouse as Warehouse = warehouseRepository.FindByLocationId(Id as int32). dim locationName as string = warehouse.location.where(fu

使用以下DDD和存储库模式,是否可以返回已包含其子数据的聚合根对象,而不是使用延迟加载

e、 g.我有一个仓库实体作为聚合根,它有一个名为location的子对象

在存储库上,我在下面有一个查询位置Id的方法,但会传回仓库实体

dim warehouse as Warehouse = warehouseRepository.FindByLocationId(Id as int32).
dim locationName as string = warehouse.location.where(function(x) x.Id = 1).firstordefault.name
当我使用warehouse.location时,EF使用代理类触发另一个DB查询来检索位置数据。
在my repository method FindByLocationId中,我可以查询location DB表并传回包含位置数据的仓库实体吗?

我猜您只想在查询中使用
include
选项

所以你会有这样的东西:

var data = (from w in context.Warehouse
            .Include("Location")
            select w).FirstOrDefault();

通常,要停止延迟加载和代理,可以在DbContext类的配置属性上设置以下属性。我倾向于在重写OnModelCreating()的方法时这样做,这样我所有的“设置”内容都在一起了

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        Configuration.LazyLoadingEnabled = false;
        Configuration.ProxyCreationEnabled = false;


        base.OnModelCreating(modelBuilder);
    }
如果您想急切地加载属性,可以使用Include()方法:

var wareHouse = (from w in ctx.WareHouses.Include("location")
                select w).FirstOrDefault();

使用include语句,它将收回仓库的所有子记录,我只想显示我正在查询的一个子记录。我尝试了以下代码,但它仍然显示所有的位置记录。Dim wareHouse=(从DataContextFactory.GetWMSDBContext.StockWarehouse中的w开始,在sl.WarehouseID上加入DataContextFactory.GetWMSDBContext.StockLocation中的sl,其中sl.Id=locationId选择w)。FirstOrDefault()