0

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.ConcurrentDictionary2.GetOrAdd(TKey key, Func2 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_01.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

1 Answer 1

0

Short answer: You're using the ForeignKey attribute wrong. Remove it and your migration should start working:

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; }
}

Long answer: In your example you're leaving out the foreign key ID property on your models. Instead you are only including the navigation property. By convention you can include the foreign key property by naming it the same as the class you are referring to and adding an ID at the end. In your example that would be:

public int CollegeId {get;set;}

The ForeignKeyAttribute can be used if you don't want to follow this convention. Lets say for example that you wanted to name the property PrimaryCollegeId instead. You could do that by using the ForeignKeyAttribute like this:

public int PrimaryCollegeId {get;set;}
[ForeignKey("PrimaryCollegeId")]
public College College {get;set;}

Usually the ForeignKeyAttribute is used if you have multiple relations between the same tables. For example if the student had a primary and secondary college both as foreign keys.

5
  • Thanks. But removing ForeignKey attribute is not working.
    – Ravi Patel
    Dec 8, 2016 at 6:32
  • What error are you seeing when you remove both of the foreignkeyattributes? It should work :) Dec 8, 2016 at 6:37
  • I just tried to Enable-Migrations with the code above, and it definitely works. Dec 8, 2016 at 6:49
  • Thanks for the answer Kasper. I solved the issue. It is because, i am referencing multiple User object in college model.
    – Ravi Patel
    Dec 8, 2016 at 6:55
  • Side note: You don't have to put the attribute on the nav property. You can put it on the foreign key itself and reference the nav property (the reverse). May 17, 2017 at 21:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.