我在使用asp.net核心项目中的Package Manager Console进行迁移时遇到了EntityFramework核心模型关系问题。
添加迁移时出现以下错误"添加迁移"。
无法确定导航属性' College.Users'所代表的关系。类型' ICollection'。手动配置关系,或从模型中忽略此属性。
完全错误
System.InvalidOperationException:无法确定导航属性' College.Users'所代表的关系。类型' ICollection'。手动配置关系,或从模型中忽略此属性。 在Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.PropertyMappingValidationConvention.Apply(InternalModelBuilder modelBuilder) 在Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.OnModelBuilt(InternalModelBuilder modelBuilder) 在Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext上下文,IConventionSetBuilder conventionSetBuilder,IModelValidator验证器) 在System.Collections.Concurrent.ConcurrentDictionary
2.GetOrAdd(TKey key, Func
2 valueFactory) 在Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel() 在Microsoft.EntityFrameworkCore.Internal.LazyRef1.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.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0
1.b__0() 在Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action) 无法确定导航属性所代表的关系。大学。用户&#39;类型&#39; ICollection&#39;。手动配置关系,或从模型中忽略此属性。
我有以下模特
public class College
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[JsonProperty("id")]
public int Id { get; set; }
[JsonProperty("name")]
public string CollegeName { get; set; }
[ForeignKey("Users")]
[JsonProperty("users")]
public ICollection<User> Users { get; set; }
}
public class User
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[JsonProperty("id")]
public int Id { get; set; }
[JsonProperty("name")]
public string UserName { get; set; }
[JsonProperty("email")]
public string UserEmail { get; set; }
[JsonProperty("phone")]
public string UserPhone { get; set; }
[ForeignKey("CollegeId")]
[JsonProperty("college")]
public College College{ get; set; }
}
请任何人对这个问题有所了解。
感谢
答案 0 :(得分:0)
简答: 您使用的ForeignKey属性错误。删除它,您的迁移应该开始工作:
public class College
{
public int Id { get; set; }
public string CollegeName { get; set; }
public ICollection<User> Users { get; set; }
}
public class User
{
public int Id { get; set; }
public string UserName { get; set; }
public string UserEmail { get; set; }
public string UserPhone { get; set; }
public College College{ get; set; }
}
答案很长: 在您的示例中,您将遗漏模型上的外键ID属性。相反,您只包含导航属性。按照惯例,您可以通过将外键属性命名为与您引用的类相同并在末尾添加ID来包含该外键属性。在您的示例中:
public int CollegeId {get;set;}
如果您不想遵循此约定,可以使用ForeignKeyAttribute。让我们说,例如,您想要命名属性PrimaryCollegeId。您可以使用ForeignKeyAttribute这样做:
public int PrimaryCollegeId {get;set;}
[ForeignKey("PrimaryCollegeId")]
public College College {get;set;}
如果在同一个表之间存在多个关系,通常会使用ForeignKeyAttribute。例如,如果学生的小学和中学都是外键。