c# - Entity Framework Core 错误在 Class.TempProperty 类型为 'object' 之前未见,当前数据库提供程序不支持

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

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

The property 'Exam.TempId' is of type 'object' which is not supported by current database provider. Either change the property CLR type or manually configure the database type for it.

在谷歌上搜索这个错误没有结果。这里有人可以帮忙吗?

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

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

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

最佳答案

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

对我来说,它是多对多 关系实体模型,带有为字段赋值的构造函数。我依靠 Visual Studio 自动为我生成属性,而 VS 做得很差,没有检测后来成为关键的属性类型。

VS 创建了 object 类型的属性,该属性过于通用,很难转化为底层数据库抽象。因此错误。

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

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

为开发人员报告的错误:#9817 .

关于c# - Entity Framework Core 错误在 Class.TempProperty 类型为 'object' 之前未见,当前数据库提供程序不支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43976692/

相关文章:

c# - 将 C# 字符串与 '==' 进行比较是一种好习惯吗?

.net - ThreadStatic 与 ThreadLocal<T> 性能 : speedups or alternatives?

entity-framework - Entity Framework 核心更新多对多

c# - 具有包含和可为空值的 Linq 查询

c# - 如何解决 Azure "Windows logins are not supported in this version of SQL Server"?

c# - 在不缓冲到 RAM 的情况下将元素复制和附加到 XML 文档

c# - 从 C# 访问注册表中的 Windows 服务参数

.net - 在 LINQ 中,.Any<> 和 .Where<> 测试记录存在性的主要区别/用途是什么

c# - 服务总线QueueClient.Receive如何工作?

c# - jQuery Mobile Datebox 不会采用默认日期值 - 尝试了所有方法