首页 > 解决方案 > 实体框架从非抽象类继承表结构,但创建所有字段

问题描述

我有几个表以下列方式声明

[Table("FileDataQuestions")]
public class QuestionFileData : FileData
{
}

FileData 在 DevExpress.Persistent.BaseImpl.EF

这些表使用单个 Id 列创建,但我希望它们使用从 FileData 继承的所有字段创建

我不能使 FileData 抽象,因为它不是我的代码。FileData 被声明为

public class FileData : IFileData, IEmptyCheckable, IObjectSpaceLink, INotifyPropertyChanged

上下文类是

using DevExpress.ExpressApp.Design;
using DevExpress.ExpressApp.EFCore.Updating;
using DevExpress.Persistent.BaseImpl.EF;
using DevExpress.Persistent.BaseImpl.EF.PermissionPolicy;
using Microsoft.EntityFrameworkCore;
namespace Exambuddy2.Module.BusinessObjects
{
    [TypesInfoInitializer(typeof(Exambuddy2ContextInitializer))]
    public class Exambuddy2EFCoreDbContext : DbContext
    {
        public Exambuddy2EFCoreDbContext(DbContextOptions<Exambuddy2EFCoreDbContext> options) : base(options)
        {
        }

        public DbSet<ModuleInfo> ModulesInfo { get; set; }
        public DbSet<ModelDifference> ModelDifferences { get; set; }
        public DbSet<ModelDifferenceAspect> ModelDifferenceAspects { get; set; }
        public DbSet<PermissionPolicyRole> Roles { get; set; }
        public DbSet<PermissionPolicyUser> Users { get; set; }
        public DbSet<FileData> FileData { get; set; }
        public DbSet<ReportDataV2> ReportDataV2 { get; set; }
        public DbSet<Source> Sources { get; set; }
        public DbSet<CourseUnit> CourseUnits { get; set; }
        public DbSet<Topic> Topics { get; set; }
        public DbSet<Question> Questions { get; set; }
        public DbSet<Answer> Answers { get; set; }

        protected override void OnModelCreating(ModelBuilder mb)
        {
            base.OnModelCreating(mb);
            mb.Entity<Topic>().HasOne(b => b.CourseUnit).WithMany(i => i.Topics);
            mb.Entity<Source>().HasOne(b => b.Topic).WithMany(i => i.Sources);
            mb.Entity<Question>().HasOne(b => b.Source).WithMany(i => i.Questions);
            mb.Entity<Answer>().HasOne(b => b.Question).WithMany(i => i.Answers);
        }
    }
}

标签: c#entity-framework-corexaf

解决方案


当我将上下文代码粘贴到问题中时,我看到了我的错误。需要为 FileDataQuestions 创建 DbSet 并为 FileData 删除


推荐阅读