簡體   English   中英

在不使用Scaffold DbContext的情況下將表添加到實體

[英]Adding A table to the entity without using Scaffold DbContext

我正在使用帶有實體框架'Scaffold-DbContext'的.Net Core。 我需要向數據庫添加一個新表,而不使用scaffold命令更新ApplicationDbContext。 所以我做了以下步驟:

  1. 使用關系創建類
  2. 在ApplicationDbContext中使用模型構建器(以生成字段)
  3. 添加虛擬DbSet

我重建我的代碼,一切正常! 但是當我使用它時它會給出:'System.Data.SqlClient.SqlException:無效的對象名'。

任何幫助都會感激不盡。 這是我的代碼:

- - 第一部分

public class PatientDoctor
{
    public int PatientDoctorId { get; set; }
    public int ?DoctorId { get; set; }
    public int ?PatientId { get; set; }

    public Patient Patient { get; set; }
    public Doctor Doctor { get; set; }
}

- - 第二部分

modelBuilder.Entity<PatientDoctor>(entity => {
            entity.HasKey(e => e.PatientDoctorId);

            entity.HasOne(e => e.Doctor).
            WithMany(p => p.PatientDoctors).
            HasForeignKey(d => d.DoctorId).
            HasConstraintName("FK_PatientDoctor_Doctor");

            entity.HasOne(e => e.Patient).
            WithMany(p => p.PatientDoctors).
            HasForeignKey(e => e.PatientId).
            HasConstraintName("FK_PatientDoctor_Patient");
        });

---第三部分: public virtual DbSet<PatientDoctor> PatientDoctors { get; set; } public virtual DbSet<PatientDoctor> PatientDoctors { get; set; }

我只改變了:

public virtual DbSet<PatientDoctor> PatientDoctors { get; set; }

至 :

public virtual DbSet<PatientDoctor> PatientDoctor { get; set; }

它在實體框架中找到了一個小錯誤

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM