使用导航属性扩展Code First Model

时间:2016-11-21 17:43:21

标签: c# entity-framework ef-code-first entity-framework-6

尝试将我的客户管理游乐场解决方案分成具有以下逻辑的模块:

  • Customer可能有Orders
  • Order的{​​{1}}可能与OrderDetails相对应,InvoiceDetails应该是Invoice的一部分。
  • OrderCustomerManagement项目的一部分,Invoice位于CustomerManagement中引用的单独项目中。

我为dbcontexts定义了两个CustomerManagement,另一个用于发票管理,后者来自第一个,这应该允许我使用CustomerManagement中定义的实体处理发票,fe以下愚蠢的导航Invoice.InvoiceDetails.First().OrderDetail.Order.Customer

CustomerManagement模型定义了客户,他的手机和其他数据; 发票管理模型定义发票,发票详细信息并继承订单以在OrderDetailInvoiceDetail之间添加新的导航属性,因为EF6不喜欢在一个模型中使用相同的简单命名类型我不得不调用扩展类型FI_OrderDetail。在这里,我希望能够像db.OrderDetails.First(od => od.Id == id).InvoiceDetail一样导航。现在,我想阻止像db.InvoiceDetails.First(invd => invd.OrderDetail.Id == id)

这样的尴尬搜索

这里出现了第一个问题,因为我没有添加任何我想要使用同一个表的真实数据,但是EF会寻找一个鉴别器,但是没有必要,因为它实际上是相同的实体,我该怎么办?我可以使用Ignore<T> DbModelBuilder,但是之后使用该忽略类型进行导航的所有其他实体都无法找到它。我应该使用其他一些方法,还是在我的场景中遗漏了一些东西?

以下是样本模型:

namespace CustomerManagement
{
    public partial class Customer
    {
        [Key]
        public int       Id              { get; set; }
        [Required]
        [Display(Name = "Given Name")]
        public string    GivenName       { get; set; }
        [Required]
        [Display(Name = "Surname")]
        public string    Surname         { get; set; }

        public virtual ICollection<Order>  Orders    { get; set; }
        public virtual IList<Phone>        Phones    { get; set; }
    }


    public class Order
    {
        [Key]
        [Column("Id", TypeName = "int")]
        public int          Id          { get; set; }
        public string       CustomerId  { get; set; }

        [Required]
        [Display(Name = "Date")]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd hh:mm}", ApplyFormatInEditMode = true)]
        public DateTime?    Date        { get; set; }

        public string       Notes       { get; set; }

        [InverseProperty("Orders")]
        public virtual Customer                     Customer      { get; set; }
        public virtual ICollection<OrderDetail>     OrderDetails  { get; set; }
    }

    public class OrderDetail
    {
        [Key]
        public int     Id              { get; set; }
        [Required]
        public int     OrderId         { get; set; }
        [Required]
        public decimal Price           { get; set; }
        [Required]
        public string  Description     { get; set; }

        [ForeignKey("OrderId")]
        public virtual  Order    Order    { get; set; }
    }

    public class CMDbContext : DbContext
    {
        public DbSet<Customer>    Customers    { get; set; }
        public DbSet<Order>       Orders       { get; set; }
        public DbSet<OrderDetail> OrderDetails { get; set; }
        public DbSet<Phone>       Phones       { get; set; }
    }
}

namespace Finance
{
    public class Invoice
    {
        [Key]
        [Column("Id", TypeName = "int")]
        public int          Id                  { get; set; }
        [Display(Name = "Customer ID")]
        public int          CustomerId          { get; set; }
        [Required]
        [Display(Name = "Date")]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd hh:mm}", ApplyFormatInEditMode = true)]
        public DateTime     Date                { get; set; }

        [ForeignKey("CustomerId")]
        public virtual CustomerManagement.Customer Customer        { get; set; }
        public virtual ICollection<InvoiceDetail>  InvoiceDetails  { get; set; }
    }

    public class InvoiceDetail
    {
        [Key]
        [Column("Id", TypeName = "int")]
        public int      Id              { get; set; }
        [Required]
        public int      InvoiceId       { get; set; }
        public string   Details         { get; set; }
        public decimal  Price           { get; set; }

        [ForeignKey("InvoiceId")]
        public virtual  Invoice        Invoice     { get; set; }
        public virtual  FI_OrderDetail OrderDetail { get; set; }
    }

    public class FI_OrderDetail : CustomerManagement.OrderDetail
    {
        public virtual ICollection<InvoiceDetail> InvoiceDetails { get; set; }
    }

    public class FIDbContext : CustomerManagement.CMDbContext
    {
        public new DbSet<FI_OrderDetail>    OrderDetails    { get; set; }
        public DbSet<Invoice>               Invoices        { get; set; }
        public DbSet<InvoiceDetail>         InvoiceDetails  { get; set; }
    }
}

0 个答案:

没有答案