我正在使用EntityFrameworkCore。
我有一个带有鉴别器的参考数据表
CREATE TABLE ReferenceData(
Id uniqueidentifier,
Discriminator varchar(255),
Value varchar(255),
SecondaryValue varchar(255),
CONSTRAINT [PK_dbo.ReferenceData] PRIMARY KEY CLUSTERED
(
[Id] ASC
)) ON [PRIMARY]
我的DbContext映射了这个
public class WeddingDbContext : DbContext
{
public DbSet<WeddingEvent> WeddingEvents { get; set; }
public DbSet<StarterChoice> StarterChoices { get; set; }
public DbSet<MainChoice> MainChoices { get; set; }
public DbSet<DessertChoice> DessertChoices { get; set; }
public WeddingDbContext(DbContextOptions<WeddingDbContext> dbContextOptions)
: base(dbContextOptions)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<ReferenceData>()
.HasDiscriminator<string>("Discriminator")
.HasValue<WeddingEvent>("WeddingEvent")
.HasValue<StarterChoice>("StarterChoice")
.HasValue<MainChoice>("MainChoice")
.HasValue<DessertChoice>("DessertChoice");
base.OnModelCreating(modelBuilder);
}
}
当我使用它时,它可以完美地工作,就像我希望的那样。
using (WeddingDbContext context = new WeddingDbContext(contextOptions))
{
foreach (WeddingEvent weddingEvent in context.WeddingEvents)
{
Console.WriteLine($"weddingevent: {weddingEvent.Value}");
}
}
我的问题是当我向DbContext添加一个新类时,它开始期望在WeddingEvent模型中有一个新列,我无法弄清楚为什么会这样。
public class WeddingDbContext : DbContext
{
// This is new
public DbSet<Invite> Invites { get; set; }
public DbSet<WeddingEvent> WeddingEvents { get; set; }
// The rest of the DbSets shown above here
}
这是新的邀请类
public class Invite
{
internal Invite(
IEnumerable<WeddingEvent> weddingEvents)
{
WeddingEvents = weddingEvents.ToList();
}
protected Invite()
{
}
public virtual ICollection<WeddingEvent> WeddingEvents { get; private set; }
}
SQL查询变为:
SELECT [r].[Id], [r].[Discriminator], [r].[Value], [r].[InviteId]
FROM [ReferenceData] AS [r]
WHERE [r].[Discriminator] = N'WeddingEvent'
为什么要尝试选择InviteId?有人可以帮忙吗?