数据库上下文结果显示 "Invalid column name 'Discriminator'" 来自继承 class
DB Context results show "Invalid column name 'Discriminator'" from inherited class
我正在尝试从连接到数据库中 table 的模型 A 检索我的控制器中的数据库上下文,但由于另一个继承自模型 A 的模型 B,结果一直出错。它包括不在数据库中的其他属性,并且不会覆盖模型 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: Cannot create a DbSet for 'ModelB' because this type is not included in the model for the context".
数据库上下文Class
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 Class
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 Class
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 结果中的所有 ModelA,而是看到了错误消息。
但是当我取消注释数据库上下文中的 "modelBuilder.Ignore()" 时,在我的控制台中我看到了我在说 "System.InvalidOperationException: Cannot create a DbSet for 'ModelB' because this type is not included in the model for the context" 之前描述的错误。所以我不能忽略它,因为它从数据库访问数据。
我通过将所有其他属性从 ModelB 放到 ModelA 来修复错误,这样我就不需要使用 ModelB 了。这现在解决了上下文问题以及利用 ModelB,因为我们不再需要利用它了。
模型 A Class(已更新)
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; }
}
}
我正在尝试从连接到数据库中 table 的模型 A 检索我的控制器中的数据库上下文,但由于另一个继承自模型 A 的模型 B,结果一直出错。它包括不在数据库中的其他属性,并且不会覆盖模型 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: Cannot create a DbSet for 'ModelB' because this type is not included in the model for the context".
数据库上下文Class
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 Class
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 Class
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 结果中的所有 ModelA,而是看到了错误消息。
但是当我取消注释数据库上下文中的 "modelBuilder.Ignore()" 时,在我的控制台中我看到了我在说 "System.InvalidOperationException: Cannot create a DbSet for 'ModelB' because this type is not included in the model for the context" 之前描述的错误。所以我不能忽略它,因为它从数据库访问数据。
我通过将所有其他属性从 ModelB 放到 ModelA 来修复错误,这样我就不需要使用 ModelB 了。这现在解决了上下文问题以及利用 ModelB,因为我们不再需要利用它了。
模型 A Class(已更新)
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; }
}
}