c# - 数据库上下文结果显示来自继承类的“无效列名'鉴别器'”
问题描述
我正在尝试从连接到数据库中的表的模型 A 中检索控制器中的数据库上下文,但由于另一个模型 B 继承自模型 A,因此结果不断出错。它包括不在其中的其他属性数据库,并且不会覆盖模型 A 中的任何属性,只是额外的属性。
同样,我试图在我的控制器中使用模型 B,并且没有为模型 A 显示上下文(因为它是模型 A 的子类),整个控制器无法运行。
我的应用程序运行 ASP.NET Core 2.1 Web API、SQL Server 2012 和 IIS Express。
我已经看到了其他类似问题的几种解决方案,比如将“[NotMapped]”放置在模型 B 上方,它是附加属性。我试过了,但没有奏效。它仍然显示标题中的错误。
我已经看到其他解决方案说通过放置“modelBuilder.Ignore()”来忽略 OnModelCreating(ModelBuilder modelBuilder) 中的模型 B。这确实消除了我最初的错误,但带来了另一个错误,即“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; }
}
}
ModelA 控制器(仅构造函数)
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 之前描述的错误,因为该类型不包含在模型中为上下文”。所以我不能忽略它,因为它从数据库中访问数据。
解决方案
我通过将我所有的附加属性从 ModelB 到 ModelA 来修复错误,这样我就不需要使用 ModelB。这现在解决了上下文问题以及利用 ModelB,因为我们不再需要使用它。
ModelA 类(更新)
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; }
}
}
推荐阅读
- android - 使用协程更新 UI 异步调用
- android - 实际图像大小如何影响颤动?
- stripe-payments - 带有 PaymentIntents 的条纹。订阅后产生新费用
- angular - Angular 6 中的材料模态
- php - 如何在单击类别时列出数据库中的类别并显示产品
- android - 操作栏菜单项:onOptionsItemSelected 未被调用
- powershell - 更改表格列宽以避免截断输出
- vb.net - 如何用数据库中的数据填充文本框
- websocket - 是否可以先创建一个 WebSocket 对象,然后设置处理程序,最后连接?
- android - 如何在 android 中设置 collapsingToolbar 的最小高度?