我正在尝试使用 Visual Studio 2022 创建一个网站。
我创建了一个数据库和一些模型,它们在数据库中有相应的表。
当我尝试创建新的迁移时,我不断收到此错误:
导航“MyfirstBlackMetalAlbum.com.Models.Users” (字典
).UserProgresses' 未找到。请加 在配置之前导航到实体类型。
但是,此导航已存在于模型和 DbContext 中:
// <auto-generated> This file has been auto-generated by EF Core Power Tools. </auto-generated>
#nullable disable
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace MyfirstBlackMetalAlbum.com.Models
{
public partial class Users
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] // This line configures it as an identity column
public int UserIntId { get; set; } // New property for the user identifier
public string Username { get; set; }
public string PasswordHash { get; set; }
public string Email { get; set; }
public byte[] ProfileImage { get; set; }
public DateTime RegistrationDate { get; set; }
public int ProgressPercentage { get; set; } // Percentage of the course completed
public int BeginnerProgress { get; set; } // Percentage of the course completed
public int IntermediateProgress { get; set; } // Percentage of the course completed
public int AdvancedProgress { get; set; } // Percentage of the course completed
public int RecordingProgress { get; set; } // Percentage of the course completed
// Navigation property for UserProgress
public ICollection<UserProgress> UserProgresses { get; set; }
}
}
这是 DBContext:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<UserProgress>(entity =>
{
entity.ToTable("UserProgress");
entity.HasKey(up => up.UserProgressId);
// Configure UserIntId and LessonId as foreign keys
entity.HasOne(up => up.User)
.WithMany(u => u.UserProgresses) // Specify the navigation property
.HasForeignKey(up => up.UserIntId)
.IsRequired();
entity.HasOne(up => up.Lesson)
.WithMany()
.HasForeignKey(up => up.LessonId)
.IsRequired();
// Configure other properties as needed
});
modelBuilder.Entity<Users>(entity =>
{
entity.ToTable("Users");
entity.HasKey(e => e.UserIntId);
entity.Property(e => e.UserIntId).UseIdentityColumn(); // This line configures UserIntId as an identity column
entity.Property(e => e.Email).IsRequired().HasMaxLength(100);
entity.Property(e => e.PasswordHash).IsRequired().HasMaxLength(128);
entity.Property(e => e.RegistrationDate).HasColumnType("datetime");
entity.Property(e => e.Username).IsRequired().HasMaxLength(50);
entity.HasMany(u => u.UserProgresses) // Specify the navigation property
.WithOne(up => up.User) // Specify the inverse navigation property in UserProgress
.HasForeignKey(up => up.UserIntId)
.IsRequired();
});
}
这是导航通向的 UserProgress 模型:
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace MyfirstBlackMetalAlbum.com.Models
{
public class UserProgress
{
[Key]
public int UserProgressId { get; set; } // Primary key for UserProgress table
[Required]
[ForeignKey("User")]
[Column("UserIntId")]
public int UserIntId { get; set; } // Foreign key pointing to Users table
[Required]
[ForeignKey("Lesson")]
[Column("LessonId")]
public int LessonId { get; set; } // Foreign key pointing to Lesson table
[Required]
[Column("IsCompleted")]
public bool IsCompleted { get; set; } // Flag indicating if the lesson is completed
public Users User { get; set; } // Navigation property to User model
public Lesson Lesson { get; set; } // Navigation property to Lesson model
}
}
不确定我在这里缺少什么。也许问题与
ContextModelSnapshot
文件有关?我尝试编辑几次,因为我在其他地方找不到任何问题。
这是一个很好的快照文件:
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using MyfirstBlackMetalAlbum.com.Models;
#nullable disable
namespace MyfirstBlackMetalAlbum.com.Migrations
{
[DbContext(typeof(MyFirstBlackMetalAlbumContext))]
partial class MyFirstBlackMetalAlbumContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.11")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("MyfirstBlackMetalAlbum.com.Models.Comment", b =>
{
b.Property<int>("CommentId")
.HasColumnType("int");
b.Property<string>("Text")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<DateTime>("Timestamp")
.HasColumnType("datetime2");
b.Property<int>("UserId")
.HasColumnType("int");
b.HasIndex("UserId");
b.ToTable("Comments");
});
modelBuilder.Entity("MyfirstBlackMetalAlbum.com.Models.Lesson", b =>
{
b.Property<int>("LessonId")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("LessonId"));
b.Property<string>("Content")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("DifficultyLevel")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("Duration")
.HasColumnType("int");
b.Property<string>("Title")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("LessonId");
b.ToTable("Lessons");
});
modelBuilder.Entity("MyfirstBlackMetalAlbum.com.Models.UserCommentsViewModel", b =>
{
b.Property<DateTime>("CreatedAt")
.HasColumnType("datetime2");
b.Property<int>("LessonId")
.HasColumnType("int");
b.Property<byte[]>("ProfilePicture")
.IsRequired()
.HasColumnType("varbinary(max)");
b.Property<string>("Text")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("UserName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("UserProgressId")
.HasColumnType("int");
// b.Property<int>("UsersUserIntId")
// .HasColumnType("int");
b.Property<int>("UserIntId")
.HasColumnType("int");
b.HasIndex("UserProgressId");
b.HasIndex("UserIntId");
b.ToTable((string)null);
b.ToView(null, (string)null);
});
modelBuilder.Entity("MyfirstBlackMetalAlbum.com.Models.UserProgress", b =>
{
b.Property<int>("UserProgressId")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("UserProgressId"));
b.Property<bool>("IsCompleted")
.HasColumnType("bit")
.HasColumnName("IsCompleted");
b.Property<int>("LessonId")
.HasColumnType("int")
.HasColumnName("LessonId");
b.Property<int>("UserIntId")
.HasColumnType("int")
.HasColumnName("UserIntId");
b.HasKey("UserProgressId");
b.HasIndex("LessonId");
b.HasIndex("UserIntId");
b.ToTable("UserProgress", (string)null);
});
modelBuilder.Entity("MyfirstBlackMetalAlbum.com.Models.Users", b =>
{
b.Property<int>("UserIntId")
.HasColumnType("int");
b.Property<int>("AdvancedProgress")
.HasColumnType("int");
b.Property<int>("BeginnerProgress")
.HasColumnType("int");
b.Property<string>("Email")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("nvarchar(100)");
b.Property<int>("IntermediateProgress")
.HasColumnType("int");
b.Property<string>("PasswordHash")
.IsRequired()
.HasMaxLength(128)
.HasColumnType("nvarchar(128)");
b.Property<byte[]>("ProfileImage")
.HasColumnType("varbinary(max)");
b.Property<int>("ProgressPercentage")
.HasColumnType("int");
b.Property<int>("RecordingProgress")
.HasColumnType("int");
b.Property<DateTime>("RegistrationDate")
.HasColumnType("datetime");
b.Property<string>("Username")
.IsRequired()
.HasMaxLength(50)
.HasColumnType("nvarchar(50)");
b.HasKey("UserIntId");
b.ToTable("Users", (string)null);
});
modelBuilder.Entity("UserComments", b =>
{
b.Property<int>("CommentId")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("CommentId"));
b.Property<DateTime>("CreatedAt")
.HasColumnType("datetime2");
b.Property<int>("LessonId")
.HasColumnType("int");
b.Property<string>("Text")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("UserIntId")
.HasColumnType("int");
b.Property<string>("UserName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("CommentId");
b.HasIndex("UserIntId");
b.ToTable("UserComment");
});
modelBuilder.Entity("MyfirstBlackMetalAlbum.com.Models.Comment", b =>
{
b.HasOne("MyfirstBlackMetalAlbum.com.Models.Users", "User")
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("User");
});
modelBuilder.Entity("MyfirstBlackMetalAlbum.com.Models.UserCommentsViewModel", b =>
{
b.HasOne("MyfirstBlackMetalAlbum.com.Models.UserProgress", "UserProgress")
.WithMany()
.HasForeignKey("UserProgressId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("MyfirstBlackMetalAlbum.com.Models.Users", "Users")
.WithMany()
//.HasForeignKey("UsersUserIntId")
.HasForeignKey("UserIntId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("UserProgress");
b.Navigation("Users");
});
modelBuilder.Entity("MyfirstBlackMetalAlbum.com.Models.UserProgress", b =>
{
b.HasOne("MyfirstBlackMetalAlbum.com.Models.Lesson", "Lesson")
.WithMany()
.HasForeignKey("LessonId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("MyfirstBlackMetalAlbum.com.Models.Users", "User")
.WithMany()
.HasForeignKey("UserIntId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
// b.HasOne("MyfirstBlackMetalAlbum.com.Models.Users", null)
// .WithMany("UserProgresses")
// .HasForeignKey("UsersUserIntId");
// .HasForeignKey("UserIntId");
b.Navigation("Lesson");
b.Navigation("User");
});
modelBuilder.Entity("MyfirstBlackMetalAlbum.com.Models.Users", b =>
{
b.HasOne("MyfirstBlackMetalAlbum.com.Models.UserProgress", "UserProgress")
.WithMany()
.HasForeignKey("UserIntId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("UserProgress");
});
modelBuilder.Entity("UserComments", b =>
{
b.HasOne("MyfirstBlackMetalAlbum.com.Models.Users", "User")
.WithMany()
.HasForeignKey("UserIntId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("User");
});
modelBuilder.Entity("MyfirstBlackMetalAlbum.com.Models.Users", b =>
{
b.Navigation("UserProgresses");
});
#pragma warning restore 612, 618
}
}
}
编辑 Dbcontext 以使导航属性更加明确,但这没有帮助。
编辑快照以执行相同操作。没有帮助。
一切都好,我通过放弃重新创建数据库解决了这个问题。