进行添加迁移时EntityFramework核心模型关系问题 [英] EntityFramework core model relationship issue while doing Add-Migration

查看:106
本文介绍了进行添加迁移时EntityFramework核心模型关系问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在asp.net核心项目中使用Package Manager Console进行迁移时,我遇到EntityFramework核心模型关系问题。

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".


无法确定由类型为 ICollection的导航属性 College.Users表示的关系。要么手动配置关系,要么从模型中忽略此属性。

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.

完全错误


System.InvalidOperationException:无法确定由类型为 ICollection的导航属性 College.Users表示的关系。手动配置关系,或从模型中忽略此属性。 Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.PropertyMappingValidationConvention.Apply(InternalModelBuilder modelBuilder)的
Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.ConventionDispatcher.OnModelBuilt(InternalModelBuilder modelBuilder)的
.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext上下文,IConventionSetBuilder ConventionSetBuilder,IModelValidator验证器)
在System.Collections.Concurrent.ConcurrentDictionary 2.GetOrAdd(TKey键,Func 2 valueFactory)Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel()
Microsoft.EntityFrameworkCore.Internal.LazyRef 1.get_Value()
在Microsoft。在Microsoft.Extensions.DependencyInjection.ServiceProvider处的Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite,ServiceProvider提供程序)
。c__DisplayClass16 _0.Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService [T](IServiceProvider提供者)b.0(ServiceProvider提供者)
Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitTransient(TransientCallSite) Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver(VisitService)提供者
)Microsoft.Extensions.DependencyInjection.ServiceProvider的
。<> c__DisplayClass16_0。< RealizeService&b; b__0(ServiceProvider提供者)Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider的
,类型serviceType )Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService [T](IServiceProvider提供者)的
,Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name,String outputDir,String contextType)
Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase中的$ b $b。Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase中的
。<> c__DisplayClass3_0
1 Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute上的.b__0()
(动作操作)
无法确定类型为 ICollection的导航属性 College.Users表示的关系。要么手动配置关系,要么从模型中忽略此属性。

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.

我有以下模型

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

请任何人对此问题有所说明。

谢谢

推荐答案

答案简短
您使用的ForeignKey属性错误。删除它,您的迁移应开始工作:

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

长答案:
在在您的示例中,您将在模型上忽略外键ID属性。相反,您仅包括navigation属性。按照约定,可以通过将外键属性命名为与您所引用的类相同的名称并在末尾添加一个ID来包含外键属性。在您的示例中,将是:

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

如果您不想遵循此约定,则可以使用ForeignKeyAttribute。例如,假设您想命名属性PrimaryCollegeId。您可以这样使用ForeignKeyAttribute来做到这一点:

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

通常如果您在同一张表之间具有多个关系,则使用ForeignKeyAttribute。例如,如果学生同时拥有一所小学和中学作为外键。

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.

这篇关于进行添加迁移时EntityFramework核心模型关系问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆