错误实体框架模型:无法确定类型为“UserProfile”的导航属性“Sanction.UserProfile”所表示的关系

问题描述 投票:0回答:0

我在我的 ASP.NET Core 应用程序中使用 Entity Framework Core 3.1.32,但在生成模型时遇到错误。具体报错信息如下:

Error invoking callback for event OnResourceStart: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidOperationException: Unable to determine the relationship represented by navigation property 'Sanction.UserProfile' of type 'UserProfile'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

这是导致问题的两个模型:

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using Models;

namespace test.Server.Models
{
    public class UserProfile
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public long Id { get; set; }

        public string SteamId { get; set; }
        public string License { get; set; }
        public string DiscordId { get; set; }
        public string Xbl { get; set; }
        public string LiveId { get; set; }
        public string Ip { get; set; }
        public long Money { get; set; }
        public DateTime PlayTime { get; set; }
        public DateTime FirstJoin { get; set; }

        public ICollection<Sanction> Sanctions { get; } = new List<Sanction>();

        [ForeignKey(nameof(Sanction))]
        public List<Sanction> GivenSanctions { get; set; }
        public UserProfilePseudonym[] UserProfilePseudonyms { get; set; } 
        public UserProfileRank[] UserProfileRanks { get; set; } 
        public bool HasPermission(Enums.Permission permission)
        {
            return UserProfileRanks.ToList().Exists(x => x.Rank.HasPermission(permission));
        }
    }
}

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace test.Server.Models
{
    public class Sanction
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public long Id { get; set; }

        public string Reason { get; set; }
        public bool IsRevoked { get; set; }
        public string RevokeReason { get; set; }
        public DateTime Date { get; set; }
        public TimeSpan? Duration { get; set; }

        [ForeignKey(nameof(UserProfile))]
        public int UserProfileId { get; set; }
        public UserProfile UserProfile { get; set; } = null;
        
        
        [ForeignKey(nameof(UserProfile))]
        public int FromUserProfileId { get; set; }
        public UserProfile FromUserProfile { get; set; }

        [ForeignKey(nameof(SanctionType))]
        public int SanctionTypeId { get; set; }
        public SanctionType SanctionType { get; set; }
    }
}

我想知道如何解决这个错误并使用 Entity Framework Core 成功生成模型。任何帮助或指导将不胜感激。谢谢。

我尝试了不同的方法,无论是使用外键还是 Icollections,但都没有用。

c# mysql entity-framework entity-framework-core
© www.soinside.com 2019 - 2024. All rights reserved.