Entity Framework:将实体添加到 Microsoft.EntityFrameworkCore.DbContext 作为 Azure 函数应用程序中的 DbSet
Entity Framework: adding an entity to Microsoft.EntityFrameworkCore.DbContext as DbSet in an Azure functions app
刚开始使用 Azure Functions 和 EF。
我有以下代码:
MyDbContext.cs
public class MyDbContext : DbContext
{
public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)
{
}
public DbSet<Badge> Badge { get; set; }
public DbSet<User> User { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfiguration(new BadgeDataConfiguration());
modelBuilder.ApplyConfiguration(new UserDataConfiguration());
}
}
BadgeDataConfiguration.cs
public class BadgeDataConfiguration : IEntityTypeConfiguration<Badge>
{
public void Configure(EntityTypeBuilder<Badge> builder)
{
builder.Property(x => x.id)
.ValueGeneratedOnAdd();
builder.Property(x => x.userid)
.HasColumnName("userid")
.IsRequired();
builder.Property(x => x.description)
.HasColumnName("description")
.IsRequired();
builder.Property(x => x.category)
.HasColumnName("category")
.IsRequired();
builder.Property(x => x.picture)
.HasColumnName("picture")
.IsRequired();
}
}
UserDataConfiguration.cs
public class UserDataConfiguration : IEntityTypeConfiguration<User>
{
public void Configure(EntityTypeBuilder<User> builder)
{
builder.Property(x => x.id)
.ValueGeneratedOnAdd();
builder.Property(x => x.username)
.HasColumnName("userid")
.IsRequired();
builder.Property(x => x.firstname)
.HasColumnName("description")
.IsRequired();
builder.Property(x => x.lastname)
.HasColumnName("category")
.IsRequired();
builder.Property(x => x.password_hash)
.HasColumnName("picture")
.IsRequired();
builder.Property(x => x.email)
.HasColumnName("category")
.IsRequired();
builder.Property(x => x.picture)
.HasColumnName("category")
.IsRequired();
builder.Property(x => x.date_created)
.HasColumnName("category")
.IsRequired();
}
}
我最初创建了 Badge
实体,并在 Azure 函数中让一切正常工作。
例如:
dbContext.Badge.ToList();
然而,一旦我为用户实体添加了所有代码,我在执行 dbContext.User.ToList();
时总是会遇到以下异常
System.Private.CoreLib: Exception while executing function: GetUser. Core Microsoft SqlClient Data Provider: Invalid object name 'User'
Badge
实体仍然正常运行。
我在这里错过了什么?
版本:
- .NET 6.0
- Azure 函数 v4
- Entity Framework Core v6.0.0
数据库 table 命名错误。
将其从“用户”更改为“用户”后一切正常。
刚开始使用 Azure Functions 和 EF。
我有以下代码:
MyDbContext.cs
public class MyDbContext : DbContext
{
public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)
{
}
public DbSet<Badge> Badge { get; set; }
public DbSet<User> User { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfiguration(new BadgeDataConfiguration());
modelBuilder.ApplyConfiguration(new UserDataConfiguration());
}
}
BadgeDataConfiguration.cs
public class BadgeDataConfiguration : IEntityTypeConfiguration<Badge>
{
public void Configure(EntityTypeBuilder<Badge> builder)
{
builder.Property(x => x.id)
.ValueGeneratedOnAdd();
builder.Property(x => x.userid)
.HasColumnName("userid")
.IsRequired();
builder.Property(x => x.description)
.HasColumnName("description")
.IsRequired();
builder.Property(x => x.category)
.HasColumnName("category")
.IsRequired();
builder.Property(x => x.picture)
.HasColumnName("picture")
.IsRequired();
}
}
UserDataConfiguration.cs
public class UserDataConfiguration : IEntityTypeConfiguration<User>
{
public void Configure(EntityTypeBuilder<User> builder)
{
builder.Property(x => x.id)
.ValueGeneratedOnAdd();
builder.Property(x => x.username)
.HasColumnName("userid")
.IsRequired();
builder.Property(x => x.firstname)
.HasColumnName("description")
.IsRequired();
builder.Property(x => x.lastname)
.HasColumnName("category")
.IsRequired();
builder.Property(x => x.password_hash)
.HasColumnName("picture")
.IsRequired();
builder.Property(x => x.email)
.HasColumnName("category")
.IsRequired();
builder.Property(x => x.picture)
.HasColumnName("category")
.IsRequired();
builder.Property(x => x.date_created)
.HasColumnName("category")
.IsRequired();
}
}
我最初创建了 Badge
实体,并在 Azure 函数中让一切正常工作。
例如:
dbContext.Badge.ToList();
然而,一旦我为用户实体添加了所有代码,我在执行 dbContext.User.ToList();
System.Private.CoreLib: Exception while executing function: GetUser. Core Microsoft SqlClient Data Provider: Invalid object name 'User'
Badge
实体仍然正常运行。
我在这里错过了什么?
版本:
- .NET 6.0
- Azure 函数 v4
- Entity Framework Core v6.0.0
数据库 table 命名错误。 将其从“用户”更改为“用户”后一切正常。