简体   繁体   English

使用Entity Framework从多个表中检索数据

[英]Retrieving data from multiple tables using Entity Framework

I want to get data from multiple tables based on the itemId of each table. 我想基于每个表的itemId从多个表中获取数据。

Currently I have an item table with an ItemId column that has a 1:m relationship with another tables called ExplicitMods : 当前,我有一个带有ItemId列的项目表,该列与另一个称为ExplicitMods表具有1:m的关系:

namespace Poe.Models
{
    public class Item
    {
        [Key]
        public string ItemId { get; set; }
        public List<ExplicitMod> ExplicitMods { get; set; }
    }
}

namespace Poe.Models
{
    public class ExplicitMod       
    {
        public ExplicitMod(string Name)
        {
            this.Name = Name;
        }

        [Key]
        public string ItemId { get; set; } 
        public string Name { get; set; }
    }
}

I also have a context with both tables set up: 我还建立了两个表的上下文:

namespace Poe.DataAccess
{
    public class DatabaseContext : DbContext
    {
        public DbSet<Item> Items { get; set; }
        public DbSet<ExplicitMod> ExplicitMod { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
        }
    }
}

I then try to call the item tables, search on a random name called "Brood Star", and join the explicitMod table: 然后,我尝试调用项目表,搜索名为“ Brood Star”的随机名称,然后加入explicitMod表:

public static void Search()
{
    //return FindItems(i);
    using (var db = new DatabaseContext())
    {
        var blogs = db.Items
                    .Where(b => b.Name.Equals("Brood Star"))
                    .Include(es => es.ExplicitMods).ToList();
        Debug.Write(blogs);
    }
}

What should I do to get the result as one table? 我应该怎么做才能得到一张桌子的结果?

I also get this error: 我也收到此错误:

System.Data.SqlClient.SqlException: 'Invalid object name 'ExplicitMod' System.Data.SqlClient.SqlException:'无效的对象名称'ExplicitMod'

Your references need a bit of adjustment. 您的参考资料需要一些调整。 For a 1-many, your ExplicitMod will need it's own PK, and a FK to Item: 对于一对多,您的ExplicitMod将需要它自己的PK和FK到Item:

public class ExplicitMod       
{
    public ExplicitMod()
    {}

    public ExplicitMod(string name)
    {
        Name = name;
    }

    [Key]
    public string ExplicitModId { get; set; } 
    [ForeignKey("Item")]
    public string ItemId{ get; set; }
    public string Name { get; set; }
    public virtual Item Item { get; set; }
}

I believe it also will need a parameterless constructor for EF to be able to construct these on the fly. 我相信它也将需要EF的无参数构造函数来即时构建它们。 You may be able to get away with a protected or private constructor. 您可能可以使用受保护的或私有的构造函数。

For Item: 对于项目:

public class Item
{
   [Key]
    public string ItemId { get; set; }
    public virtual ICollection<ExplicitMod> ExplicitMods { get; private set; } = new List<ExplicitMod>();
}

Initializing the collection and using a private setter is recommended to save steps when creating new entities to populate, and having external code setting a collection reference to a new, untracked set of entities. 建议初始化集合并使用私有设置器,以节省创建要填充的新实体时的步骤,并使外部代码将集合引用设置为新的,未跟踪的实体集。 (For unit testing, I typically mark the setter internal to allow unit tests to set up stubs) (对于单元测试,我通常将设置器标记为内部,以允许单元测试设置存根)

I'd strongly recommend using an int or Guid for the PK/FK fields rather than strings. 我强烈建议为PK / FK字段而不是字符串使用intGuid If you want to have unique identifying strings then add these as additional columns /w unique constraints. 如果要具有唯一的标识字符串,则将它们添加为附加列/ w唯一约束。 Using numeric or UUID keys are a form of "meaningless keys" which save indexing space and make modifying values easier without worrying about changing/invalidating data relationships accidentally. 使用数字或UUID键是“无意义的键”的一种形式,它节省了索引空间,并使修改值更容易,而不必担心意外更改/使数据关系无效。

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

相关问题 从表中获取多个数据使用Entity Framework数据模型 - Get multiple data from tables Using Entity Framework data model 使用实体框架从数据库中检索数据 - Retrieving data from database using Entity Framework 如何使用实体框架从多个表中检索数据 - How to retrieve data from multiple tables using Entity Framework 使用实体框架从多个表中选择 - Select from multiple tables using Entity Framework 使用Entity Framework从数据库检索数据时出现WebAPI问题 - WebAPI issue while retrieving data from database using Entity Framework 使用实体框架将数据保存在一个事务中的多个表中 - saving data in multiple tables in one transaction using entity framework 使用实体框架为多个表保存数据的更好方法 - Better approach for saving data for multiple tables using entity framework 使用Entity Framework,Lambda Expressions和Repositorys模式从多个表中搜索数据 - Searching data from multiple tables using Entity framework, Lambda Expressions and Repositorys pattern 使用实体框架和存储库模式从ASP.NET MVC中的多个表中检索数据 - Retrieve data from multiple tables in ASP.NET MVC using Entity Framework and a repository pattern 使用 LINQ 和实体框架在一个 SQL 查询中从多个表中提取数据 - Pull data from multiple tables in one SQL query using LINQ and Entity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM