【问题标题】:SqlException: Invalid object nameSqlException:无效的对象名称
【发布时间】:2017-06-18 15:44:23
【问题描述】:

我正在使用 EF 核心开始一个新项目。 我在 MSSQL 服务器上有一个现有数据库,我运行此命令以包含其用于 EF 的结构。

Scaffold-DbContext "Server={My Server};Database={My DB};Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer

这已经为我创建了模型类,例如:

public partial class Cameras
{
    public int CameraId { get; set; }
    public string Description { get; set; }
}

以及以下上下文类:

public partial class SetupToolContext : DbContext
{
    public virtual DbSet<Cameras> Cameras { get; set; }

    public SetupToolContext(DbContextOptions<SetupToolContext> options) : base(options)
    { }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Cameras>(entity =>
        {
            entity.HasKey(e => e.CameraId)
                .HasName("PK_Cameras");

            entity.Property(e => e.Description).HasColumnType("varchar(500)");
        });
    }
}

我的解决方案中有 4 层:

  • 业务逻辑
  • DAL
  • 接口(用于依赖注入)
  • API(控制器)

这是我的代码中流程的样子:

控制器类

public class ValuesController : Controller
{
    ICameraRepository cameraRepository;

    public ValuesController(ICameraRepository cameraRepository)
    {
        this.cameraRepository = cameraRepository;
    }

    [HttpGet]
    public IEnumerable<Cameras> Get()
    {
        //ERROR: "Invalid object name 'Cameras'."
        return cameraRepository.List();
    }
}

CameraRepository 类

public class CamerasRepository : GenericRepository<Cameras>, ICameraRepository
{
    private readonly SetupToolContext dbContext;

    public CamerasRepository(SetupToolContext dbContext) : base(dbContext)
    {
        this.dbContext = dbContext;
    }
}

public interface ICameraRepository : IRepository<Cameras>
{ }

GenericRepository 类(尝试关注Repository Pattern

public class GenericRepository<T> : IRepository<T> where T : class
{
    private readonly SetupToolContext dbContext;

    public GenericRepository(SetupToolContext dbContext)
    {
        this.dbContext = dbContext;
    }

    public IEnumerable<T> List()
    {
        return dbContext.Set<T>().AsEnumerable();
    }
}

public interface IRepository<T>
{
    IEnumerable<T> List();
}

和启动类ConfigureServices方法

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<SetupToolContext>(options => options.UseSqlServer(Configuration.GetConnectionString("SQLServerConnection")));
    services.AddMvc();
    services.AddScoped<ICameraRepository, CamerasRepository>();
}

问题是我收到一个错误:"Invalid object name 'Cameras'."

这个过程有什么问题?

【问题讨论】:

  • 数据库中的表名是什么?是cameras吗?
  • 名称是“相机”。它是由Scaffold-DbContext 命令自动生成的。
  • 名称Cameras是EF自动生成的,但是表名呢?只是为了确认..是相机吗?因为这似乎是问题所在。
  • 请不要在题名中打标签,见What are tags, and how should I use them?
  • 数据库中的表名也是“Cameras”。我又检查了一遍……

标签: c# asp.net-core entity-framework-core


【解决方案1】:

说起来有点尴尬,但我仍然会发布这个答案,以防其他人因为没有注意到这个“次要”细节而出现同样的错误。 我有 2 个数据库,结果我忘记将连接字符串中的数据库名称更改为正确的数据库名称。 我在Scaffold-DbContext 命令中更改了它,只是忘记在连接字符串中也更改它...

{
  "ConnectionStrings": {
    "SQLServerConnection": "Server=localhost;Database={I had the wrong db name here};Trusted_Connection=True;"
  }
}

所以改成正确的就解决了。

【讨论】:

  • 别觉得太尴尬。找到了这个答案,发现我犯了同样的错误
猜你喜欢
  • 2017-06-14
  • 2021-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-29
相关资源
最近更新 更多