【问题标题】:How to code C# MVC model for many to many join table for sqlite database migration { edited }如何为 SQLite 数据库迁移的多对多连接表编写 C# MVC 模型
【发布时间】:2019-08-15 01:36:02
【问题描述】:

我正在创建一个 sqlite 数据库来跟踪分配给团队的用户。这将意味着多对多表关系。我需要帮助制作迁移模型。

users table: 
    public class User
    {
        public int Id { get; set; }
        public string Username { get; set; }
        public ICollection<Photo> Photos { get; set; }
        public virtual ICollection<UserTeam> UserTeams { get; set; }
    }

team table:
    public class Team
    {
        public int Id { get; set; }
        public string Name { get; set; } 
        public User User { get; set; }
        public int UserId { get; set; }
        public virtual ICollection<UserTeam> UserTeams{ get; set; }
    }

UserTeam Table:
    public class UserTeam
    {
        public User User { get; set; }
        public int UserId{ get; set; }
        public Team Team{ get; set; }  
        public int TeamId{ get; set; } //... I have this one for owner of the team
        public string Title { get; set; }

    }

DataContext:
    public class DataContext : DbContext
    {
        public DataContext(DbContextOptions<DataContext> options) : base (options) {}

        public DbSet<Value> Values { get; set; }

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

        public DbSet<Photo> Photos { get; set; }

        public DbSet<Team> Teams { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<UserTeam>()
                .HasKey(ut => new { ut.UserId, ut.TeamId });  
            modelBuilder.Entity<UserTeam>()
                .HasOne(ut => ut.User)
                .WithMany(u => u.UserTeams)
                .HasForeignKey(ut => ut.UserId);  
            modelBuilder.Entity<UserTeam>()
                .HasOne(ut => ut.Team)
                .WithMany(t => t.UserTeams)
                .HasForeignKey(ut => ut.TeamId);
        }
    }

从我读到的声明我 ICollection 在团队类和用户类上将强制创建连接表。但是,当我尝试迁移时,我收到此消息:无法确定由“ICollection”类型的导航属性“User.Teams”表示的关系。手动配置关系,或使用“[NotMapped]”属性或使用“OnModelCreating”中的“EntityTypeBuilder.Ignore”忽略此属性。

任何关于如何在 sqlite 中正确制作连接表的建议都会有所帮助

【问题讨论】:

    标签: c# asp.net-mvc database sqlite many-to-many


    【解决方案1】:

    看起来您正在使用EF Core 2.x。有必要在模型中定义实体。这意味着您必须通过在 OnModelCreating 方法中覆盖它来定义关系。

    我会通过创建另一个表来保存用户和用户所属团队的引用来进一步规范您的结构,反之亦然。我会将此表称为UserTeam(因为缺乏更好的命名)

    所以我会有类似的东西:

    public class User
    {
        //....omitted
        public virtual ICollection<UserTeam> UserTeams { get; set; }
    }
    
    public class UserTeam
    {      
        public int UserId{ get; set; }
        public User User { get; set; }
        public int TeamId{ get; set; }
        public Team Team{ get; set; }
    }
    
    public class Team
    {
        //.... omitted
        public virtual ICollection<UserTeam> UserTeams{ get; set; }
    }
    

    需要配置 UserTeam 表,以便 EFCore 可以成功映射它。这是我们定义多对多关系的地方

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<UserTeam>()
            .HasKey(ut => new { ut.UserId, ut.TeamId });  
        modelBuilder.Entity<UserTeam>()
            .HasOne(ut => ut.User)
            .WithMany(u => u.UserTeams)
            .HasForeignKey(ut => ut.UserId);  
        modelBuilder.Entity<UserTeam>()
            .HasOne(ut => ut.Team)
            .WithMany(t => t.UserTeams)
            .HasForeignKey(ut => ut.TeamId);
    }
    

    【讨论】:

    • 谢谢! OnModelCreating 进入什么文件?似乎找不到我是否需要创建一个新类或将其放入 DbContext 或其他地方。
    • 这是从Microsoft.EntityFrameworkCore 包导入的DbContext 类的可覆盖方法。因此,更正它应该转到您的应用程序上下文所在的位置。你应该有一个扩展DbContext的上下文类,把它放在那里:)
    • 我编辑了我的问题。这是它应该看起来的样子吗?迁移运行但只是想确定一下。 C# 中的超级绿色。你不是指这个 DbContext 中的 Db 上下文吗?公共类 DbContext:IDisposable、IInfrastructure、IDbContextDependencies、IDbSetCache、IDbQueryCache、IDbContextPoolable
    • @tonsar 是的,您在编辑/更新的问题中是如何做到的。希望这会有所帮助:)
    猜你喜欢
    • 1970-01-01
    • 2016-12-19
    • 2011-10-04
    • 1970-01-01
    • 1970-01-01
    • 2020-08-21
    • 2020-10-16
    • 2020-01-28
    • 1970-01-01
    相关资源
    最近更新 更多