【问题标题】:ASP.NET Core MVC code-first complex relation with enumerated value and index具有枚举值和索引的 ASP.NET Core MVC 代码优先复杂关系
【发布时间】:2021-09-16 16:56:18
【问题描述】:

我有一个需要复杂关系的项目:

public enum InputFileTypes
{
    unknown = 0,
    ConfirmationFile = 1,
    PrestationFile = 2,
    EOCChoiceFile = 3,
    EOCReplaceFile = 4,
    CareStaffFile = 5,
    JobCreationFile = 6,
    NurseTitleFile = 7
};

public class InputFile
{
    [Key]
    public int Id { get; set; }
    public string Filename { get; set; }
    public InputFileTypes InputFileType { get; set; }
    public Guid ScraperUploadClassId { get; set; }
    public ScraperUploadClass ScraperUploadClass { get; set; }

    public DateTime FileDateTime
    {
        get
        {
            return File.GetCreationTime(Filename);
        }
    }
}

public class ScraperUploadClass
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public Guid ID { get; set; }
    public Regions Region { get; set; }
    public virtual ICollection<InputFile> ConfirmationFiles { get; set; }
    public virtual ICollection<InputFile> PrestationFiles { get; set; }
    public InputFile EocChoiceFile { get; set; }
    public InputFile EocReplaceFile { get; set; }
    public InputFile CareStaffFile { get; set; }
    public InputFile JobCreationFile { get; set; }
    public InputFile NurseTitlesFile { get; set; }
}

modelBuilder.Entity<InputFile>()
    .HasIndex(c => new { c.InputFileType, c.ScraperUploadClassId });

问题是ModelBuilderInputFile 有一个组合主键:inputfiletypeScraperUploadclassid

Scraperclass 需要与输入文件有多个关系:

  • 属性 EocCHoiceFile 一对一,其中 InputFile.InputFileType == InputFileTypes.EOCChoiceFile
  • 属性 EOCReplaceFile 一对一,其中 InputFile.InputFileType == InputFileTypes.EOCReplaceFile

...

  • Confirmationsfiles 列表的一对多,其中 InputFile.InputFileType == ConfirmationFile
  • 最后是 PrestationsFiles 列表的一对多,其中 InputFile.InputFileType == PrestationFile

谁能告诉我如何在DbContext.OnModelCreating 方法中做到这一点?

我似乎没有使用modelBuilder.... 添加迁移时似乎总是会导致问题。

【问题讨论】:

    标签: c# asp.net-core-mvc ef-code-first ef-code-first-mapping


    【解决方案1】:

    自己找到了答案,只使用 ScraperUploadClass 中的一个虚拟列表,而不是为单独的类型创建 getter 函数

    public class InputFile
        {
            [Key]
            public int Id { get; set; }
            public string Filename { get; set; }
            public InputFileTypes InputFileType { get; set; }
            public Guid ScraperUploadClassID { get; set; }
        }
        public class ScraperUploadClass
        {
            [Key]
            [DatabaseGenerated(DatabaseGeneratedOption.None)]
            public Guid ID { get; set; }
            public Regions Region { get; set; }
            public virtual ICollection<InputFile> InputFiles { get; set; }
        }
    
                modelBuilder.Entity<InputFile>()
                                .Property(c => c.InputFileType)
                                .HasConversion<string>();
    
                modelBuilder.Entity<InputFile>()
                    .HasOne(d => d.ScraperUploadClass)
                    .WithMany(p => p.InputFiles)
                    .HasForeignKey(d => d.ScraperUploadClassID);
    
                modelBuilder.Entity<InputFile>()
                 .HasIndex(t => new { t.ScraperUploadClassID, t.InputFileType, t.Filename })
                 .IsUnique();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-17
      • 2012-06-25
      • 2013-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多