AJ_*_*AJ_ 3 c# sql-server entity-framework asp.net-core
我正在制作一个幻想游戏应用程序。现在,一个团队可以有多个球员,但也可以有多个用户(例如联赛)。当我尝试将集合添加到团队表时,出现以下错误(见下文)。我试图在线查找解决方案,看来此问题是由多对多关系引起的。但是,我不知道这怎么可能。一个团队可以有多个用户,但是一个用户只能有一个团队。解决方案是创建一个联接表,但是当我只需要让每个表上的列联接到另一个表时,为什么要在每个表上创建一个列以指向包含数据的联接表?有谁知道导致此错误的原因及其解决方法?
System.InvalidOperationException: Unable to determine the relationship represented by navigation property 'Team.Manager' of type 'User'. Either manually configure the relationship, or ignore this property from the model. at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.PropertyMappingValidationConvention.Apply(InternalModelBuilder modelBuilder)
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.OnModelBuilt(InternalModelBuilder modelBuilder)
at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel()
at Microsoft.EntityFrameworkCore.Internal.LazyRef`1.get_Value()
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass16_0.<RealizeService>b__0(ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitTransient(TransientCallSite transientCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitTransient(TransientCallSite transientCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass16_0.<RealizeService>b__0(ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
at Microsoft.EntityFrameworkCore.Design.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)
at Microsoft.EntityFrameworkCore.Tools.Cli.MigrationsAddCommand.Execute(CommonOptions commonOptions, String name, String outputDir, String context, String environment, Action`1 reporter)
at Microsoft.EntityFrameworkCore.Tools.Cli.MigrationsAddCommand.<>c__DisplayClass0_0.<Configure>b__0()
at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
at Microsoft.EntityFrameworkCore.Tools.Cli.Program.Main(String[] args)
Unable to determine the relationship represented by navigation property 'Team.Manager' of type 'User'. Either manually configure the relationship, or ignore this property from the model.
Run Code Online (Sandbox Code Playgroud)
Team.cs
public class Team
{
[Key]
public int ID { get; set; }
public int ManagerID { get; set; }
public string Name { get; set; }
[ForeignKey("ManagerID")]
public virtual User Manager { get; set; }
public virtual ICollection<Player> Players { get; set; }
// Unable to determine the relationship represented by navigation property 'Team.Manager' of type 'User'
public virtual ICollection<User> TeamMebers { get; set; }
public Team()
{
TeamMebers = new List<User>();
Players = new List<Player>();
}
}
Run Code Online (Sandbox Code Playgroud)
User.cs
public class User
{
[Key]
public int ID { get; set; }
public int? TeamID { get; set; }
public string UserName { get; set; }
public string Name { get; set; }
public bool ShowAsPm { get; set; }
public string Deactiviated { get; set; }
[ForeignKey("TeamID")]
public virtual Team Team { get; set; }
// How many hours they played
public virtual ICollection<Log> Hours { get; set; }
public User()
{
Hours = new List<Log>();
}
}
Run Code Online (Sandbox Code Playgroud)
Log.cs
public class Log
{
[Key]
public int ID { get; set; }
public int? GameID { get; set; }
public int? ResultID { get; set; }
public int? UserID { get; set; }
public string SmNumber { get; set; }
public decimal? Duration { get; set; }
public DateTime LogDate { get; set; }
[ForeignKey("GameID")]
public virtual Game Game { get; set; }
[ForeignKey("ResultID")]
public virtual GameResult Result { get; set; }
[ForeignKey("UserID")]
public virtual User User { get; set; }
public Log() { }
}
Run Code Online (Sandbox Code Playgroud)
Context.cs
protected override void OnModelCreating(ModelBuilder builder)
{
//builder.Entity<Team>().HasMany(t => t.TeamMebers).WithOne(u => u.Team).HasForeignKey(u => u.ID);
//builder.Entity<User>().HasOne(u => u.Team).WithMany(t => t.TeamMebers).HasForeignKey(t => t.ID);
builder.Entity<Team>().ToTable("Teams");
builder.Entity<User>().ToTable("Users");
builder.Entity<Log>().ToTable("Logs");
}
Run Code Online (Sandbox Code Playgroud)
更新资料
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Team>().ToTable("Teams");
builder.Entity<User>().ToTable("Users");
builder.Entity<User>().HasOne(u => u.Team).WithMany(u => u.TeamMebers);
builder.Entity<Log>().ToTable("Logs");
}
Run Code Online (Sandbox Code Playgroud)
例外
System.InvalidOperationException: The current CSharpHelper cannot scaffold literals of type 'System.Collections.Immutable.ImmutableSortedDictionary`2[System.Reflection.PropertyInfo,System.Type]'. Configure your services to use one that can. at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpHelper.UnknownLiteral(Object value)
at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateAnnotation(IAnnotation annotation, IndentedStringBuilder stringBuilder)
at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateEntityTypeAnnotations(IEntityType entityType, IndentedStringBuilder stringBuilder)
at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateEntityType(String builderName, IEntityType entityType, IndentedStringBuilder stringBuilder)
at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateEntityTypes(String builderName, IReadOnlyList`1 entityTypes, IndentedStringBuilder stringBuilder)
at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.Generate(String builderName, IModel model, IndentedStringBuilder stringBuilder)
at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpMigrationsGenerator.GenerateMetadata(String migrationNamespace, Type contextType, String migrationName, String migrationId, IModel targetModel)
at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.ScaffoldMigration(String migrationName, String rootNamespace, String subNamespace)
at Microsoft.EntityFrameworkCore.Design.MigrationsOperations.AddMigration(String name, String outputDir, String contextType)
at Microsoft.EntityFrameworkCore.Tools.Cli.MigrationsAddCommand.Execute(CommonOptions commonOptions, String name, String outputDir, String context, String environment, Action`1 reporter)
at Microsoft.EntityFrameworkCore.Tools.Cli.MigrationsAddCommand.<>c__DisplayClass0_0.<Configure>b__0()
at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
at Microsoft.EntityFrameworkCore.Tools.Cli.Program.Main(String[] args)
The current CSharpHelper cannot scaffold literals of type 'System.Collections.Immutable.ImmutableSortedDictionary`2[System.Reflection.PropertyInfo,System.Type]'. Configure your services to use one that can.
Run Code Online (Sandbox Code Playgroud)
您遇到的“问题”将在以下情况中说明:
Unable to determine the relationship represented by navigation property 'Team.Manager' of type 'User'. Either manually configure the relationship, or ignore this property from the model.
Run Code Online (Sandbox Code Playgroud)
消息提示,解决方案是手动定义此关系。
builder.Entity<User>().HasOne(u => u.Team).WithMany(u => u.TeamMembers);
Run Code Online (Sandbox Code Playgroud)
但是,这不能完全解决您的问题,因为其中一个成员是经理,这不能解决问题。
更好的解决方案是使用以下中间类,该中间类将处理关系,同时还将成员之一标记为Manager。这仍然不能解决每个团队只有一个Manager约束的问题,但是距离越来越近。
public class TeamMember {
public int MemberId {get;set;}
public int TeamId {get;set;}
public bool IsManager {get;set;}
public virtual User Member {get;set;}
public virtual Team Team {get;set;}
}
//Either add an ID or use fluent API to define the key to be a combination of player/teamid
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9966 次 |
| 最近记录: |