[英]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字段而不是字符串使用int
或Guid
。 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.