List在EntityFramework Entity中如何工作?

huangapple go评论96阅读模式
英文:

How does List work in EntityFramework Entity?

问题

我有2个数据模型:

  1. public class Role
  2. {
  3. int Id;
  4. string Name;
  5. }

  1. public class User
  2. {
  3. int Id;
  4. string Name;
  5. List<Role> Roles;
  6. }

这是我的DbContext:

  1. public class DatabaseContext : DbContext
  2. {
  3. public DbSet<User> Users => Set<User>();
  4. public DbSet<Role> Roles => Set<Role>();
  5. protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  6. {
  7. optionsBuilder.UseSqlite("Data Source=database.sqlite");
  8. }
  9. }

但是我在List<Role>方面遇到了问题,当我从查看器中检查数据库数据时,我看到Users表中没有任何用户的Roles,但Roles表中有UserID属性,它似乎可以工作,但只存储一个ID,我该如何修复它?

我需要有多个角色和具有其中一些角色的用户。

英文:

I have 2 data models:

  1. public class Role
  2. {
  3. int Id;
  4. string Name;
  5. }

and

  1. public class User
  2. {
  3. int Id;
  4. string Name;
  5. List&lt;Role&gt; Roles;
  6. }

there is my DbContext:

  1. public class DatabaseContext : DbContext
  2. {
  3. public DbSet&lt;User&gt; Users =&gt; Set&lt;User&gt;();
  4. public DbSet&lt;Role&gt; Roles =&gt; Set&lt;Role&gt;();
  5. protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
  6. {
  7. optionsBuilder.UseSqlite(&quot;Data Source=database.sqlite&quot;);
  8. }
  9. }

but i have troubles with List&lt;Role&gt;, when i check db data from viewer, i see that Users table does not have any Roles in users, but Roles table has property UserID, it kinda works but only 1 id stored there, how can i fix it?

I need to have multiple roles
and users that has some of these roles

答案1

得分: 0

> 它有点起作用,但只有一个ID被存储在那里,我该如何修复它?

这取决于你实际想要的。根据应用程序的要求,通常用户可以被分配一个角色,或者用户和角色之间存在多对多的关系。

如果你希望用户只能拥有一个角色,那么你应该反转关系,即角色具有用户列表:

  1. public class Role
  2. {
  3. // ...
  4. public List<User> Users { get; set; } = new();
  5. }
  6. public class User
  7. {
  8. public int Id { get; set; }
  9. public string Name { get; set; }
  10. }

对于多对多的情况,有两个主要选项 - 在双方都指定List,并让EF内部创建联接表,或者显式配置联接表(EF Core关于多对多的文档)。对于第一种情况,你的实体可以如下所示:

  1. public class Role
  2. {
  3. // ...
  4. public List<User> Users { get; set; } = new();
  5. }
  6. public class User
  7. {
  8. public int Id { get; set; }
  9. public string Name { get; set; }
  10. public List<Role> Roles { get; set; } = new();
  11. }
英文:

> it kinda works but only 1 id stored there, how can I fix it?

It depends on what you actually want. Depending on the app requirements usually user can be assigned only one role or there is a many-to-many relationship between users and roles.

In case if you want user to have only one role then you should reverse the relationship i.e. role has list of users:

  1. public class Role
  2. {
  3. // ...
  4. public List&lt;User&gt; Users { get; set; } = new();
  5. }
  6. public class User
  7. {
  8. public int Id { get; set; }
  9. public string Name { get; set; }
  10. }

For the many-to-many case there are two main options - specify List on both sides and let EF internally create the join table or explicitly configure the join table (EF Core docs on many-to-many). For the first case your entities can look like the following:

  1. public class Role
  2. {
  3. // ...
  4. public List&lt;User&gt; Users { get; set; } = new();
  5. }
  6. public class User
  7. {
  8. public int Id { get; set; }
  9. public string Name { get; set; }
  10. public List&lt;Role&gt; Roles { get; set; } = new();
  11. }

huangapple
  • 本文由 发表于 2023年2月9日 02:10:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75390046.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定