I am facing an EntityFramework core model relation issue while doing migration using Package Manager Console in a asp.net core project.
Getting below error while adding migration "Add-Migration".
Unable to determine the relationship represented by navigation property 'College.Users' of type 'ICollection'. Either manually configure the relationship, or ignore this property from the model.
Full Error
System.InvalidOperationException: Unable to determine the relationship represented by navigation property 'College.Users' of type 'ICollection'. 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.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() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action) Unable to determine the relationship represented by navigation property 'College.Users' of type 'ICollection'. Either manually configure the relationship, or ignore this property from the model.
I have following models
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; }
}
Please anyone throw some light on the issue.
Thanks