【问题标题】:Entity Framework Inherit table structure from non abstract class , but create all the fields实体框架从非抽象类继承表结构,但创建所有字段
【发布时间】:2020-12-20 23:24:20
【问题描述】:

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

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

【问题讨论】:

  • 您是否尝试过手动映射列?在这种情况下,我会回避自动映射或脚手架。
  • 请编辑您的问题以包含上下文类。另外,您是否尝试过使用docs.microsoft.com/en-us/ef/core/modeling/inheritance 中提到的HasBaseType((Type)null)?根据您当前对上下文类的设置,在数据库中创建了哪些表以及它们的外观应该如何?
  • Xaf 将FileData 用于项目中的所有文件以及引用它的其他实体。如果你想拥有自己的FileQuestionData,对我来说,唯一的方法就是添加properties并实现FileData类实现的interfaces
  • 抱歉,回答迟了。

标签: c# entity-framework-core xaf


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 2016-03-26
    • 2013-06-10
    • 2019-01-08
    相关资源
    最近更新 更多