【问题标题】:Entity Framework Core error not seen before Class.TempProperty is of type 'object' which is not supported by current database providerClass.TempProperty 之前未出现的实体框架核心错误是当前数据库提供程序不支持的“对象”类型
【发布时间】:2017-05-15 10:05:51
【问题描述】:

我在 ASP .NET MVC Core 应用程序中使用具有流畅 API 实体配置的 Entity Framework Core code-first。我的代码目前正在编译,但是当我在包管理器控制台中运行 add-migration 时,它给出了以下错误:

属性“Exam.TempId”的类型为“object”,不受支持 由当前的数据库提供商。更改属性 CLR 类型或 手动为其配置数据库类型。

在 Google 中搜索此错误不会产生任何结果。有人可以帮忙吗?

“Exam”是我的域模型中的一个类,但它没有“TempId”属性,所以我猜这是 Entity Framework 正在添加的东西。它确实有一个“Id”属性,但类型是 int,而不是 object。

我将首先分享考试类和考试配置类。如果需要,我可以分享更多代码。如果您能提供任何解决问题的建议,我将不胜感激。

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

【问题讨论】:

  • 能否展示基类(如ActivatableEntity 等)
  • 层次结构中的任何一个基类都肯定有TypeId
  • 感谢您的关注!当然我可以分享基类。我已经编辑了原始帖子以包含它们。
  • 我刚刚在整个解决方案中搜索了“TempId”这个词。未找到任何结果。
  • public int Id { get; private set; }

标签: c# .net entity-framework entity-framework-core


【解决方案1】:

我遇到了同样的问题,这让我很困惑。但幸运的是我使用了版本控制,所以我能够追踪问题的原因。

对我来说,它是many-to-many 关系实体模型,它带有将值分配给字段的构造函数。我依靠 Visual Studio 自动为我生成属性,而 VS 做得很糟糕,没有检测到后来成为键的属性类型。

VS 创建了object 类型的属性,它过于通用,几乎无法转换为底层数据库抽象。因此出现错误。

我同意,完全不是描述性的,希望他们会在未来的版本中解决这个问题。

所以尝试搜索object类型的属性并检查它们是否用作键,如果是,请尝试将它们替换为您的数据库提供商支持的特定类型。

为开发者报告的错误:#9817

【讨论】:

    猜你喜欢
    • 2021-11-12
    • 2023-01-15
    • 1970-01-01
    • 1970-01-01
    • 2020-11-29
    • 2020-06-07
    • 1970-01-01
    • 2021-05-20
    • 1970-01-01
    相关资源
    最近更新 更多