在Class.TempProperty类型为' object'之前未见过的实体框架核心错误当前数据库提供程序不支持

时间:2017-05-15 10:05:51

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

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

  

属性' Exam.TempId'属于'对象'这是不受支持的   由当前数据库提供商。要么改变属性CLR类型,要么   为它手动配置数据库类型。

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

"考试"是我的域模型中的一个类,但它没有" TempId"属性,所以我想这是实体框架正在添加的东西。它确实有一个" Id"属性,但类型是int,而不是对象。

我将首先分享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; }
    }
}

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题而且让我很困惑。但幸运的是我正在使用版本控制,因此我能够追踪问题的原因。

对我而言,many-to-many关系实体模型与构造函数一起为字段赋值。我依靠Visual Studio自动为我生成属性,而VS做得很差,没有检测到后来成为关键属性的属性。

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

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

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

开发者报告的错误:#9817