数据库上下文结果显示继承类的"Invalid column name 'Discriminator'"



我正在尝试从连接到数据库中表的模型 A 检索控制器中的数据库上下文,但由于另一个模型 B 继承自模型 A 而导致结果不断出错。它包括数据库中没有的其他属性,并且不会覆盖模型 A 中的任何属性,只是额外的属性。

同样,我正在尝试在我的控制器中使用模型 B,并且没有为模型 A 显示上下文(因为它是模型 A 的子类),整个控制器无法运行。

我的应用程序运行 ASP.NET Core 2.1 Web API,SQL Server 2012和IIS Express。

我已经看到了与此类似的其他问题的几种解决方案,例如将"[NotMapped]"放在模型B及其附加属性之上。我已经试过了,但没有奏效。它仍然在标题中显示错误。

我已经看到其他解决方案说忽略OnModelCreation(ModelBuilder modelBuilder)中的模型B,方法是输入"modelBuilder.Ignore()"。这确实消除了我最初的错误,但带来了另一个错误,即"System.InvalidOperationException:无法为'ModelB'创建 DbSet,因为此类型未包含在上下文的模型中"。

数据库上下文类

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DT.Models;
namespace DT.Models
{
public class DTContext : DbContext
{
public DTContext(DbContextOptions<DTContext> options) : base(options) { }
public DbSet<ModelA> ModelAs { get; set; }
public DbSet<ModelB> ModelBs { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<ModelA>().ToTable("ModelA");
//modelBuilder.Ignore<ModelB>();
}
}
}

模型 A 类

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
namespace DT.Models
{
[Description("GET or DELETE using Id")]
public class ModelA
{
public int Id { get; set; }
public string HotfixID { get; set; }
public string Description { get; set; }
public string ExtendedDescription { get; set; }
public string BugTrackCases { get; set; }
public string Type { get; set; }
public string AppVersion { get; set; }
public DateTime HotfixDate { get; set; }
public string HotfixLocation { get; set; }
public int ClientID { get; set; }
public List<DeploySite> DeploySites { get; set; }
public List<FBCase> BugTrackCaseList { get; set; }
public int? OriginalId { get; set; }
public string CaseType { get; set; }
public bool HasSQL { get; set; }
public bool HasConfig { get; set; }
public bool HasLibraries { get; set; }
public bool HasWebApps { get; set; }
}
}

B类

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Threading.Tasks;
namespace DT.Models
{
[NotMapped]
public class ModelB : ModelA
{
[NotMapped]
public string ClientName { get; set; }
[NotMapped]
public string Status { get; set; }
[NotMapped]
public bool Complete { get; set; }
[NotMapped]
public DateTime LastUpdatedDate { get; set; }
}
}

模型 A 控制器(仅构造函数)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using DT.Models;
using System.Configuration;
using System.Data;
namespace DT.Controllers
{
[Route("api/modela")]
[ApiController]
public class ModelAsController : ControllerBase
{
private readonly DTContext _context;
public ModelAsController(DTContext context)
{
_context = context;
}
}
}

而不是在构造函数中_context的结果中看到所有 ModelAs,那是我看到错误消息的时候。

但是,当我在数据库上下文中取消注释"modelBuilder.Ignore()"时,在我的控制台中,我看到了我在说"System.InvalidOperationException:无法为'ModelB'创建 DbSet,因为此类型未包含在上下文的模型中"之前描述的错误。所以我不能忽略它,因为它访问数据库中的数据。

我通过将模型 B 中的所有附加属性放入模型 A 来修复错误,这样我就不需要使用 ModelB。这现在解决了上下文问题以及使用 ModelB,因为我们不再需要使用它。

模型 A 类(已更新)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
namespace DT.Models
{
[Description("GET or DELETE using Id")]
public class ModelA
{
public int Id { get; set; }
public string HotfixID { get; set; }
public string Description { get; set; }
public string ExtendedDescription { get; set; }
public string BugTrackCases { get; set; }
public string Type { get; set; }
public string AppVersion { get; set; }
public DateTime HotfixDate { get; set; }
public string HotfixLocation { get; set; }
public int ClientID { get; set; }
public List<DeploySite> DeploySites { get; set; }
public List<FBCase> BugTrackCaseList { get; set; }
public int? OriginalId { get; set; }
public string CaseType { get; set; }
public bool HasSQL { get; set; }
public bool HasConfig { get; set; }
public bool HasLibraries { get; set; }
public bool HasWebApps { get; set; }
[NotMapped]
public string ClientName { get; set; }
[NotMapped]
public string Status { get; set; }
[NotMapped]
public bool Complete { get; set; }
[NotMapped]
public DateTime LastUpdatedDate { get; set; }
}
}

最新更新