Entity Framework 多表之间的关系

3

我的项目中有两个表,分别为用户和发票行。

要求一个 发票行 可以有一个称为审核员的 用户

我的模型如下:

public class InvoiceLine : IEntity
    {
        public virtual int Id { get; set; }
        public virtual int? CheckerId { get; set; }
        public virtual string CreatedByUserName { get; set; }
        public virtual DateTime CreatedDateTime { get; set; }
        public virtual string LastModifiedByUserName { get; set; }
        public virtual DateTime? LastModifiedDateTime { get; set; }

        // Navigation properties
        public virtual User Checker{ get; set; }
}



public class User : IEntity
    {
        public int Id { get; set; }
        public string CreatedByUserName { get; set; }
        public DateTime CreatedDateTime { get; set; }
        public string LastModifiedByUserName { get; set; }
        public DateTime? LastModifiedDateTime { get; set; }

        //Navigation properties
        public virtual ICollection<InvoiceLine> InvoiceLines { get; set; }
    }

所以这很好,我从UserInvoiceLine有一个0..1到多的关系。

这意味着使用Linq,我可以通过以下方式获取用户需要检查的发票行:

user.InvoiceLines

然而还有另外一个要求,即一个 InvoiceLine 还必须有一个 Auditor ,因此我对 InvoiceLine 进行了修改:

public class InvoiceLine : IEntity
    {
        public virtual int Id { get; set; }
        public virtual int? CheckerId { get; set; }
        public virtual int? AuditorId { get; set; }
        public virtual string CreatedByUserName { get; set; }
        public virtual DateTime CreatedDateTime { get; set; }
        public virtual string LastModifiedByUserName { get; set; }
        public virtual DateTime? LastModifiedDateTime { get; set; }

        // Navigation properties}
        public virtual User Checker { get; set; }
        public virtual User Auditor { get; set; }
}

所以我真正想要的是去做 IT 技术方面的工作:
user.InvoiceLines

获取 检查员审计员,或者可以通过以下方式单独获取:

user.CheckerInvoiceLines
user.AuditorInvoiceLines

尽管我从user.InvoiceLines那里得到了null,但这是可以理解的。

请问有人可以指导我如何使用Linq从User中获取InvoiceLines吗?

编辑更新:

我的模型配置代码如下:

public class VectorCheckContext : DbContext
    {
        ...

        public DbSet<InvoiceLine> InvoiceLines { get; set; }
        public DbSet<User> Users { get; set; }

        ...

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
        }
    }

你能发布一下你的模型配置代码吗? - Eranga
1个回答

7

当EF无法通过惯例解决关系时,您需要使用流畅的映射来配置关系。

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();

        //other mappings

        modelBuilder.Entity<InvoiceLine>()
           .HasOptional(i => i.Checker)
           .WithMany(u => u.CheckerInvoiceLines)
           .HasForeignKey(i => i.CheckerId);

        modelBuilder.Entity<InvoiceLine>()
           .HasOptional(i => i.Auditor)
           .WithMany(u => u.AuditorInvoiceLines)
           .HasForeignKey(i => i.AuditorId);
    }

太好了,这正是我想要的。谢谢! - AnonyMouse

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接