Dotnet Web API项目自动生成控制器的POST方法问题



我试图通过为我的实体框架生成的DB表模型创建控制器来学习Dotnet 5 Web API。数据库结构非常简单,包含3个表(PaymentDetail, CustomerDetail和ClubStateDetail)。通过EF生成的DBContext文件具有如下所示的构造函数:

public partial class PaymentDetailDBContext : DbContext
{
public PaymentDetailDBContext()
{
}
public PaymentDetailDBContext(DbContextOptions<PaymentDetailDBContext> options)
: base(options)
{
}
public virtual DbSet<ClubStateDetail> ClubStateDetails { get; set; }
public virtual DbSet<CustomerDetail> CustomerDetails { get; set; }
public virtual DbSet<PaymentDetail> PaymentDetails { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. You can avoid scaffolding the connection string by using the Name= syntax to read it from configuration - see https://go.microsoft.com/fwlink/?linkid=2131148. For more guidance on storing connection strings, see http://go.microsoft.com/fwlink/?LinkId=723263.
optionsBuilder.UseSqlServer("Server=.;Database=PaymentDetailDB;Trusted_Connection=True;");
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasAnnotation("Relational:Collation", "SQL_Latin1_General_CP1_CI_AS");
modelBuilder.Entity<ClubStateDetail>(entity =>
{
entity.HasKey(e => e.ClubStateId);
entity.ToTable("ClubStateDetail");
entity.Property(e => e.ClubStateId)
.ValueGeneratedNever()
.HasColumnName("ClubStateID");
entity.Property(e => e.Name)
.IsRequired()
.HasMaxLength(50);
});
modelBuilder.Entity<CustomerDetail>(entity =>
{
entity.HasKey(e => e.CardOwnerId);
entity.ToTable("CustomerDetail");
entity.Property(e => e.CardOwnerId)
.ValueGeneratedNever()
.HasColumnName("CardOwnerID");
entity.Property(e => e.Name)
.IsRequired()
.HasMaxLength(100);
entity.HasOne(d => d.ClubStateNavigation)
.WithMany(p => p.CustomerDetails)
.HasForeignKey(d => d.ClubState)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_CustomerDetail_ClubStateDetail");
});
modelBuilder.Entity<PaymentDetail>(entity =>
{
entity.ToTable("PaymentDetail");
entity.Property(e => e.PaymentDetailId).HasColumnName("PaymentDetailID");
entity.Property(e => e.CardNumber).HasMaxLength(15);
entity.Property(e => e.CardOwnerId).HasColumnName("CardOwnerID");
entity.Property(e => e.Cvv)
.HasMaxLength(6)
.HasColumnName("CVV");
entity.Property(e => e.Expirationdate).HasMaxLength(6);
});
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}

首先,因为每个客户都必须有一个ClubState,所以我创建了ClubState控制器,它的POST方法如下所示:

// POST: api/ClubStateDetails
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPost]
public async Task<ActionResult<ClubStateDetail>> PostClubStateDetail(ClubStateDetail clubStateDetail)
{
_context.ClubStateDetail.Add(clubStateDetail);
await _context.SaveChangesAsync();
return CreatedAtAction("GetClubStateDetail", new { id = clubStateDetail.ClubStateId }, clubStateDetail);
}

现在,我转到Swagger UI来测试这个API,它要求请求体JSON数据具有以下格式(请注意,我已经为我的clubStateId和名称设置了值):

{
"clubStateId": 1,
"name": "Gold",
"customerDetails": [
{
"cardOwnerId": 0,
"name": "string",
"clubState": 0
}
]
}

因为到目前为止我没有任何客户,所以我保留了这些值不变,它们保持原样,因为它们在Swagger提供的示例JSON数据中(我意识到,这可能是一个问题,但由于我现在没有任何客户,我不确定该放在那里)。

现在,当我尝试执行这个POST请求时,我得到一个错误,说

Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details.
---> Microsoft.Data.SqlClient.SqlException (0x80131904): Invalid column name 'ClubStateNavigationClubStateId'.

ClubStateNavigationClubStateId不是我的ClubState表中的一列,但是当我查看CustomerDetail控制器的JSON数据时,我确实看到了以下内容:

{
"cardOwnerId": 0,
"name": "string",
"clubState": 0,
"clubStateNavigation": {
"clubStateId": 0,
"name": "string",
"customerDetails": [
null
]
}
}

所以我的问题是,当我还没有任何客户准备好时,我如何使用我的API发布数据?

Microsoft.EntityFrameworkCore。DbUpdateException:更新表项时发生错误。有关详细信息,请参阅内部异常。——比;Microsoft.Data.SqlClient.SqlException (0x80131904):无效的列名'ClubStateNavigationClubStateId'.

这意味着数据库与EF DbContext的配置不匹配。EF认为有一列ClubStateNavigationClubStateId在数据库中不存在

你从数据库中逆向工程模型了吗?

最新更新