2017-06-18 112 views
-1

我正在开始使用EF内核的新项目。 我有一个MSSQL服务器上的现有数据库,我运行此命令以包含其EF结构。SqlException:无效的对象名称

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层,在我的解决方案:

  • BusinessLogic
  • DAL
  • 接口(依赖注入)
  • API(控制器)

这里是流动的样子在我的代码:

Controller类

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'."

Here is a screenshot of the error 这个过程出了什么问题?

+0

数据库中表的名称是什么?它是“相机”吗? – silkfire

+0

名称是“相机”。它是由'Scaffold-DbContext'命令自动生成的。 –

+0

摄像头的名称是由EF自动生成的,但表名是什么?只是为了确认..是相机吗?因为这似乎是问题所在。 – jpgrassi

回答

0

这有点令人尴尬,但我仍然会发布这个答案,以防其他人会因为没有注意到这个“小”细节而导致同样的错误。 我有2个数据库,事实证明我忘了将连接字符串中的数据库名称更改为正确的数据库名称。 我改变了它在Scaffold-DbContext命令,只是忘了也改变它在连接字符串中...

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

因此改变它正确的解决了这个问题。

-1

尝试删除DbSet<Cameras>上的virtual关键字,因为EntityFramework Core目前不支持延迟加载。做一个ToList()而不是AsEnumerable()

+0

这与问题无关。 'virtual'和'AsEnumerable()'都不会产生这种错误。 –