【发布时间】:2022-07-19 16:40:11
【问题描述】:
我正在使用 C# (.NET) 开发一个项目,我想进行初始迁移,但遇到了问题。我尝试了很多解决方案,但仍然出现错误。
AppDBContext:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
object value = modelBuilder.Entity<Actor_Movie>().HasKey(am => new
{
am.ActorId,
am.MovieId,
}
);
modelBuilder.Entity<Actor_Movie>().HasOne(m => m.Movie).WithMany(am => am.Actors_Movies).HasForeignKey(m => m.MovieId);
modelBuilder.Entity<Actor_Movie>().HasOne(m => m.Actor).WithMany(am => am.Actors_Movies).HasForeignKey(m => m.ActorId);
base.OnModelCreating(modelBuilder);
}
MovieCategory.cs:
namespace eTickets.Data.Enums;
public class MovieCategory
{
}
错误:
PM> add-migration initial
Build started...
Build succeeded.
System.InvalidOperationException: The entity type 'MovieCategory' requires a primary key to be defined. If you intended to use a keyless entity type, call 'HasNoKey' in 'OnModelCreating'. For more information on keyless entity types, see https://go.microsoft.com/fwlink/?linkid=2141943.
at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.ValidateNonNullPrimaryKeys(IModel model, IDiagnosticsLogger`1 logger)
at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.Validate(IModel model, IDiagnosticsLogger`1 logger)
Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
The entity type 'MovieCategory' requires a primary key to be defined. If you intended to use a keyless entity type, call 'HasNoKey' in 'OnModelCreating'. For more information on keyless entity types, see https://go.microsoft.com/fwlink/?linkid=2141943.
让我知道我做错了什么。
【问题讨论】:
标签: c# .net asp.net-mvc visual-studio