简体   繁体   English

实体框架-自定义导航属性以抽象联接

[英]Entity Framework - customize navigation properties to abstract joins

I have looked through similar questions here, but not seen this specific scenario. 我在这里浏览了类似的问题,但没有看到这种特定情况。

Using EF 6 Code First, I have three tables, A, B and C. The relationship is A => B = 1:M, and B=>C = 1:1 首先使用EF 6代码,我有三个表A,B和C。关系为A => B = 1:M,B => C = 1:1

The end result in this schema is that there is an implicit 1:M between A and C. 该模式的最终结果是在A和C之间存在一个隐式1:M。

I do NOT want the consumer of the Entity Framework model to know about B. Ideally they would have a 1:M Navigation property from A to C (and I'd like to be able to surface this Entity Model through Web API and OData as IQueryable) 我不希望实体框架模型的使用者了解B。理想情况下,他们将具有从A到C的1:M导航属性(并且我希望能够通过Web API和OData展示此实体模型,如下所示)可查询的)

How could I do this? 我该怎么办?

If I add a custom [NotMapped] property to A which a collection of C, I have no way of populating C within the getter of that property because the entity doesn't know about its context. 如果我向A添加一个自定义[NotMapped]属性(其中包含C的集合),则由于该实体不知道其上下文,因此无法在该属性的getter中填充C。

Anyone have any ideas as to how to implement an IQueryable where A has a navigation property to C and B is 'abstracted' out of existence? 任何人都对如何实现IQueryable有任何想法,其中A具有到C的导航属性,而B被“抽象”了吗?

EDIT 编辑

Attempted to put the following into the code first entity A: 尝试将以下内容放入代码中的第一个实体A:

[NotMapped]
public ICollection<C> Cs
{
        get { return this.Bs.Select(b => b.C) as ICollection<C>; }
}

But got this error: The navigation property 'C' is not a declared property on type 'A'. 但是出现了此错误:导航属性“ C”不是类型“ A”上的已声明属性。 Verify that it has not been explicitly excluded from the model and that it is a valid navigation property. 验证尚未将其明确排除在模型之外,并且它是有效的导航属性。

Thanks. 谢谢。

Here is an example. 这是一个例子。

public static class OrderDAL
{
    public static Order Get(int key)
    {
        using (var context = new AppContext())
        {
            var order = context.Orders.Include(a => a.OrderDetails.Select(b => b.Information)).FirstOrDefault(a => a.Id == key);
            // Fills C.
            order.OrderDetailAdditionalInformation = order.OrderDetails.Select(b => b.Information).ToArray();
            // Hides information about B.
            foreach (var information in order.OrderDetailAdditionalInformation)
            { information.OrderDetail = null; }
            order.OrderDetails = null;
            return order;
        }
    }
}
public class AppContext : DbContext
{
    public DbSet<Order> Orders { get; set; }
}
// A
public class Order
{
    public int Id { get; set; }
    public DateTime Date { get; set; }
    public ICollection<OrderDetail> OrderDetails { get; set; }
    [NotMapped]
    public ICollection<OrderDetailAdditionalInformation> OrderDetailAdditionalInformation { get; set; }
}
// B, one A many B
public class OrderDetail
{
    public int Id { get; set; }
    public int Qty { get; set; }
    public string Item { get; set; }
    public int OrderId { get; set; }
    public Order Order { get; set; }
    public OrderDetailAdditionalInformation Information { get; set; }
}
// C, one B one C
public class OrderDetailAdditionalInformation
{
    [ForeignKey("OrderDetail")]
    public int Id { get; set; }
    public int Width { get; set; }
    public int Height { get; set; }
    public int Long { get; set; }
    public OrderDetail OrderDetail { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM