简体   繁体   English

实体框架基本属性映射

[英]Entity Framework base property mapping

Hello I have a problem with my mapping. 您好,我的映射存在问题。

I have classes in hierarchical structure: 我有分层结构的类:

  1. TaxYear TaxYear
  2. Owner 所有者
  3. EconomyUnit EconomyUnit
  4. Report 报告
  5. ReportItem ReportItem
  6. Invoice 发票

All this classes have a base class "BaseEntity" and a parent property and parent_id property. 所有这些类都有一个基类“ BaseEntity”以及一个父属性和parent_id属性。

The invoice parent property is not from type "ReportItem" because "Onwer" has a seperate invoice list so the type is "BaseEntity". 发票父属性不是来自“ ReportItem”类型,因为“ Onwer”具有单独的发票列表,因此类型为“ BaseEntity”。

This works fine I can use as parent "ReportItem" and "Owner". 这可以正常工作,我可以用作父级“ ReportItem”和“所有者”。

But now I want to "move" the invoices between parents. 但是现在我想“移动”父母之间的发票。 At this moment only from "ReportItem" to "ReportItem". 此时仅从“ ReportItem”到“ ReportItem”。

Set the new parent: 设置新的父代:

foreach (var invoice in oldReportItem.Invoices)
{
    invoice.Parent = newReportItem;
}
ctx.SaveChanges();

This works without errors but not correctly. 这可以正常工作,但不会出现错误。 EF created three columns for my parent property. EF为我的父级属性创建了三列。

  • Owner_Id Owner_Id
  • ReportItem_Id ReportItem_Id
  • Parent_Id PARENT_ID

If you insert a invoice then both columns will get the same Id. 如果插入发票,则两列将获得相同的ID。 Works fine but if you set a new parent then "Onwer_Id" or ReportItem_Id will be set to null and Parent_Id will only be updated. 工作正常,但如果您设置了新的父代,则“ Onwer_Id”或ReportItem_Id将设置为null,而Parent_Id将仅被更新。

There is my problem. 这是我的问题。 I think my mapping is wrong. 我认为我的映射是错误的。 Can anyone help me? 谁能帮我?

Fluent-Api 流利的API

modelBuilder.Entity<ReportItem>()
    .HasOptional(p => p.Parent)
    .WithMany(p => p.Items)
    .HasForeignKey(k => k.ParentId)
    .WillCascadeOnDelete(false);

BaseEntity: BaseEntity:

public abstract class BaseEntity
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }

    public abstract String GetName();

    public DateTime? CreateDate { get; set; }
    public DateTime? LastEditDate { get; set; }

    public BaseEntity DeepCopy()
    {
        return (BaseEntity)MemberwiseClone();
    }
}

Owner: 所有者:

public class Owner : BaseEntity
{
    public Owner()
    {
        Items = new ObservableCollection<EconomyUnit>();
        PrivateInvoices = new ObservableCollection<Invoice>();
    }

    [Required]
    public String Name { get; set; }

    [Required]
    public TaxYear Year { get; set; }
    public Guid? YearId { get; set; }

    public ICollection<EconomyUnit> Items { get; private set; }
    public ICollection<Invoice> PrivateInvoices { get; private set; }

    public override string GetName()
    {
        return Name;
    }
}

ReportItem: ReportItem:

public class ReportItem : BaseEntity
{
    public ReportItem()
    {
        Items = new ObservableCollection<Invoice>();
    }

    [Required]
    public String Name { get; set; }

    public ICollection<Invoice> Items { get; private set; }

    public Guid? ParentId { get; set; }
    public Report Parent { get; set; }
}

Invoice: 发票:

public class Invoice: BaseEntity
{
    public String Name { get; set; }

    public Guid? ParentId { get; set; }

    public BaseEntity Parent { get; set; }

}

Thank you Lyror 谢谢你莱尔

Ok, let's start with the OP. 好的,让我们从OP开始。

I have classes in hierarchical structure: 我有分层结构的类:

TaxYear Owner EconomyUnit Report ReportItem Invoice 纳税人所有者经济单位报告报告项目发票

Your class structures indicate that TaxYear is optional so it cannot be a parent (by definition). 您的类结构表明TaxYear是可选的,因此它不能是父类(根据定义)。 Some of your classes are missing so I cannot determine if any other relationships are flawed. 您的某些班级缺失,因此我无法确定其他任何关系是否存在缺陷。 Taking your statement at face value, your classes would be more like this (extraneous fields removed for clarity) 以您的陈述为准,您的类将更像这样(为清晰起见,删除了外部字段)

public abstract class BaseEntity - Unchanged

public class Owner : BaseEntity
{
    // Parent navigation    
    public Guid YearId { get; set; }
    public virtual TaxYear Year { get; set; }

    // Child navigation
    public virtual ICollection<EconomyUnit> Units { get; private set; }
}

public class EconomicUnit : BaseEntity
{
    // Parent navigation    
    public Guid OwnerId { get; set; }
    public virtual Owner Owner { get; set; }

    // Child navigation
    public virtual ICollection<Report> Reports { get; private set; }
}

public class Report : BaseEntity
{
    // Parent navigation    
    public Guid UnitId { get; set; }
    public virtual EconomicUnit Unit { get; set; }

    // Child navigation    
    public virtual ICollection<ReportItem> Items { get; private set; }
}

public class ReportItem : BaseEntity
{
    // Parent navigation    
    public Guid ReportId { get; set; }
    public virtual Report Report { get; set; }

    // Child navigation    
    public virtual ICollection<Invoice> Invoices { get; private set; }
}

public class Invoice : BaseEntity
{
    // Parent navigation    
    public Guid ReportItemId { get; set; }
    public virtual ReportItem ReportItem { get; set; }
}

If you want to see a list of Invoices for an Owner, try this on: 如果要查看所有者的发票列表,请尝试以下操作:

dbContext.Invoices.Where(a => a.ReportItem.Report.EconomicUnit.Owner.Id == <parameter>).Include(b => b.ReportItem.Report.EconomicUnit.Owner.TaxYear).ToList();

Of course, you can select into a new DTO object inverting the relationship Owner ==> Invoices. 当然,您可以选择一个新的DTO对象,以反转所有者==>发票的关系。

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

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