在Class.TempProperty之前未見到的實體框架核心錯誤是“對象”類型,當前數據庫提供程序不支持該類型

.net c# entity-framework entity-framework-core

我在ASP .NET MVC Core應用程序中使用Entity Framework Core code-first使用流暢的API實體配置。我的代碼目前正在編譯,但是當我在Package Manager控制台中運行add-migration時,它會給出以下錯誤:

屬性“Exam.TempId”的類型為“object”,當前數據庫提供程序不支持該屬性。更改屬性CLR類型或手動為其配置數據庫類型。

在Google上搜索此錯誤不會產生任何結果。有人可以幫忙嗎?

“考試”是我的域模型中的一個類,但它沒有“TempId”屬性,所以我猜這是Entity Framework正在添加的東西。它確實有一個“Id”屬性,但類型是int,而不是object。

我將首先分享Exam類和Exam配置類。如果需要,我可以分享更多代碼。我非常感謝您提供解決問題的建議。

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace MySite.Core.Models
{
    public class Exam : ActivatableEntity
    {
        private int _numberOfQuestionsToBeAttempted;

        private Exam()
        {
            Topics = new Collection<Topic>();
        }

        public Exam(IUser createdByUser,
            string name,
            string description,
            double timeAllowedInMinutes,
            bool shuffleTopicsTogether = true) :
            base(createdByUser)
        {
            Name = name;
            Description = description;
            Topics = new Collection<Topic>();
            TimeAllowedInMinutes = timeAllowedInMinutes;
            ShuffleTopicsTogether = shuffleTopicsTogether;
        } 

        public string Name { get; private set; }

        public string Description { get; private set; }

        public double TimeAllowedInMinutes { get; private set; }

        public bool ShuffleTopicsTogether { get; private set; }

        public IEnumerable<Question> PossibleQuestions
        {
            get
            {
                return Topics.SelectMany(t => t.PossibleQuestions);
            }
        }

        public int NumberOfQuestionsToBeAttempted
        {
            get
            {
                if (_numberOfQuestionsToBeAttempted != 0) return _numberOfQuestionsToBeAttempted;
                foreach (Topic topic in Topics)
                {
                    _numberOfQuestionsToBeAttempted += topic.NumberOfQuestionsToBeAttempted;
                }
                return _numberOfQuestionsToBeAttempted;
            }
        }

        public IEnumerable<Topic> Topics { get;  }

        public void Update(IUser updatedByUser, string name, string description, double timeAllowedInMinutes, bool shuffleTopicsTogether = true)
        {
            Name = name ?? throw new ArgumentNullException(nameof(name));
            Description = description;
            TimeAllowedInMinutes = timeAllowedInMinutes;
            ShuffleTopicsTogether = shuffleTopicsTogether;
            Update(updatedByUser);
        }
    }
}

考試配置類

using MySite.Core.Models;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace MySite.Persistence.EntityConfiguration
{
    public class ExamConfiguration
    {
        public ExamConfiguration(EntityTypeBuilder<Exam> entityBuilder)
        {
            entityBuilder.HasKey(e => e.Id);

            entityBuilder.HasOne(e => (ApplicationUser)e.CreatedByUser)
                .WithMany()
                .HasForeignKey(e => e.CreatedByUserId)
                .OnDelete(DeleteBehavior.SetNull);

            entityBuilder.HasOne(e => (ApplicationUser)e.LastUpdatedByUser)
                .WithMany()
                .HasForeignKey(e => e.LastUpdatedByUserId)
                .OnDelete(DeleteBehavior.SetNull);

            entityBuilder.Property(e => e.Name).IsRequired().HasMaxLength(50);

            entityBuilder.Property(e => e.Description).IsRequired().HasMaxLength(250);

            entityBuilder.HasMany(e => e.Topics)
                .WithOne(t => t.Exam).OnDelete(DeleteBehavior.Cascade);
        }
    }
}

根據海報的要求,我正在為以下基類添加代碼:

using System;

namespace MySite.Core.Models
{
    public abstract class ActivatableEntity :
        UpdatableCreatableEntity,
        IActivatable
    {
        protected ActivatableEntity() { }

        protected ActivatableEntity(IUser createdByUser) : base(createdByUser) { }

        public int? LastActivatedByUserId { get; private set; }
        public IUser LastActivatedByUser { get; private set; }
        public DateTime? WhenLastActivated { get; private set; }
        public int? LastDeactivatedByUserId { get; private set; }
        public IUser LastDeactivatedByUser { get; private set; }
        public DateTime? WhenLastDeactivated { get; private set; }
        public bool IsActive { get; private set; }

        protected virtual void Activate(IUser activatedByUser)
        {
            LastActivatedByUser = activatedByUser ?? throw new ArgumentNullException(nameof(activatedByUser));

            LastActivatedByUserId = activatedByUser.Id;
            WhenLastActivated = DateTime.Now;
            IsActive = true;
        }

        protected virtual void Deactivate(IUser deactivatedByUser)
        {
            LastDeactivatedByUser = deactivatedByUser ?? throw new ArgumentNullException(nameof(deactivatedByUser));

            LastDeactivatedByUserId = deactivatedByUser.Id;
            WhenLastDeactivated = DateTime.Now;
            IsActive = false;
        }
    }

    public abstract class UpdatableCreatableEntity :
        CreatableEntity,
        IUpdatable
    {
        protected UpdatableCreatableEntity() { }

        protected UpdatableCreatableEntity(IUser createdByUser) : base(createdByUser) { }

        public int? LastUpdatedByUserId { get; private set; }
        public IUser LastUpdatedByUser { get; private set; }
        public DateTime? WhenLastUpdated { get; private set; }

        protected virtual void Update(IUser updatedByUser)
        {
            LastUpdatedByUser = updatedByUser ?? throw new ArgumentNullException(nameof(updatedByUser));
            LastUpdatedByUserId = updatedByUser.Id;
            WhenLastUpdated = DateTime.Now;
        }
    }

    public abstract class CreatableEntity :
        IIdentifiable,
        ICreatable
    {
        protected CreatableEntity() { }

        protected CreatableEntity(IUser createdByUser)
        {
            CreatedByUser = createdByUser ?? throw new ArgumentNullException(nameof(createdByUser));
            CreatedByUserId = createdByUser.Id;
            WhenCreated = DateTime.Now;
        }

        public int Id { get; private set; }
        public int? CreatedByUserId { get; private set; }
        public DateTime WhenCreated { get; private set; }
        public IUser CreatedByUser { get; private set; }
    }
}

熱門答案

我遇到了同樣的問題而且讓我很困惑。但幸運的是我正在使用版本控制,所以我能夠追踪問題的原因。

對我來說,它是帶有構造函數many-to-many關係實體模型,它為字段賦值。我依靠Visual Studio自動為我生成屬性,並且VS做得不好,沒有檢測到後來成為關鍵屬性的屬性。

VS創建了object類型的object ,它太通用了,幾乎無法轉換為底層數據庫抽象。因此錯誤。

我同意,非常沒有描述性,希望他們能在未來的版本中解決這個問題。

因此,嘗試搜索對object類型的屬性並檢查它們是否用作鍵,如果是,請嘗試用數據庫提供程序支持的特定類型替換它們。

報告錯誤的開發人員: #9817



Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow