简体   繁体   English

实体框架平行一对多关系

[英]Entity Framework Parallel One-to-Many Relationship

I've been looking around to try to create a parallel one-to-many relationship in EF. 我一直在寻找尝试在EF中建立平行的一对多关系。 Heres what I mean: 这是我的意思:

I've got two classes, Transaction and User: 我有两个类,Transaction和User:

public class Transaction
{
    public int ID { get; set; }

    public string ExecutorID { get; set; }
    public virtual User Executor { get; set; }

    public string ConsumerID { get; set; }
    public virtual User Consumer { get; set; }
}

public class User
{
    public UserRole Role { get; set; }
    public string Name { get; set; }

    public string Id { get; set; }
    public string Pin { get; set; }

    public virtual List<Transaction> ExecutedTransactions { get; set; }
    public virtual List<Transaction> Transactions { get; set; } 
}

If you haven't seen it already, I've got two links between the objects, Transactions and Executed Transactions . 如果您还没有看到它,那么我在对象之间有两个链接,即TransactionsExecuted Transactions My question is how can I tell EF to differentiate between the two? 我的问题是如何告诉EF区分两者?

Transactions should point to all Transaction where User.ID == Transaction.ConsumerID and ExecutedTransactions where User.ID == Transaction.ExecutorID 交易应指向所有Transaction ,其中User.ID == Transaction.ConsumerIDExecutedTransactions其中User.ID == Transaction.ExecutorID

Here are the classes where Food and Book are derived from Item . 这是从Item派生FoodBook的类。

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<Food> Foods { get; set; }
    public ICollection<Book> Books { get; set; }
}
public abstract class Item
{
    public int Id { get; set; }
    public string Name { get; set; }
}
public class Food : Item
{
    public int CookedById { get; set; }
    [ForeignKey("CookedById")]
    public Person CookedBy { get; set; }
}
public class Book : Item
{
    public int AuthorId { get; set; }
    [ForeignKey("AuthorId")]
    public Person Author { get; set; }
}

Option A Table Per Hierarchy 每个层次的选项A表

public class AppContext : DbContext
{
    public DbSet<Person> People { get; set; }
    public DbSet<Item> Items { get; set; }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Person>().HasMany(p => p.Foods).WithRequired(f => f.CookedBy).WillCascadeOnDelete(false);
    }
}

Result would be 2 tables , table Person and Item that has automatically Discriminator column that will be filled with Food and Book . 结果将是2个表 ,表PersonItem具有自动Discriminator列,该表将用FoodBook填充。

To query you need to filter from Items by its type. 要进行查询,您需要按Items的类型对其进行过滤。

using (var context = new AppContext())
{
    var foods = context.Items.Where(item => item is Food).ToArray();
    var books = context.Items.Where(item => item is Book).ToArray();
}

Option B Table Per Concrete Class 每个具体类别的选项B表

public class AppContext : DbContext
{
    public DbSet<Person> People { get; set; }
    public DbSet<Food> Foods { get; set; }
    public DbSet<Book> Books { get; set; }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Person>().HasMany(p => p.Foods).WithRequired(f => f.CookedBy).WillCascadeOnDelete(false);
    }
}

Result would be 3 tables , table Person , Food and Book . 结果将是3个表 ,表PersonFoodBook

To query you can directly take from Foods and Books. 要查询,您可以直接从食品和书籍中获取。

using (var context = new AppContext())
{
    var foods = context.Foods.ToArray();
    var books = context.Books.ToArray();
}

Option C Table Per Type 选项C每种类型的表格

public class AppContext : DbContext
{
    public DbSet<Person> People { get; set; }
    public DbSet<Item> Items { get; set; }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Person>().HasMany(p => p.Foods).WithRequired(f => f.CookedBy).WillCascadeOnDelete(false);
        modelBuilder.Entity<Food>().ToTable("Foods");
        modelBuilder.Entity<Book>().ToTable("Books");
    }
}

Result would be 4 Tables , table Person , Item , Food and Book . 结果将是4个表 ,表PersonItemFoodBook Table Food and Book will have relationship to table Ttem . 餐桌FoodBook将与餐桌Ttem有关系。

The query is the same like Table Per Hierarchy. 该查询与“每个层次的表”相同。

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

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