moh*_*317 4 c# entity-framework asp.net-core-2.0
我试图在.net core 2.0 Web应用程序中连接到localDB。我已经使用SQL Express创建了本地数据库。
我的appsettings.json看起来像这样
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
},
"ConnectionStrings": {
"DefaultConnection": "Data Source=(LocalDb)\\MSSQLLocalDB;Initial Catalog=Shopping;Integrated Security=True;Connect Timeout=60;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
}
}
Run Code Online (Sandbox Code Playgroud)
我的上下文文件如下所示
public class ProductContext : DbContext
{
public ProductContext(DbContextOptions<ProductContext> options)
:base(options)
{
}
public DbSet<Product> Products { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我的启动文件通过这样的上下文
services.AddDbContext<ProductContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
Run Code Online (Sandbox Code Playgroud)
当我尝试从数据库中获取产品时
try
{
var d = await _context.Products.ToListAsync();
return View(d);
}
catch (Exception e)
{
}
Run Code Online (Sandbox Code Playgroud)
我抛出了这个异常
“无效的对象名称'Products'。”
我也在这里添加我的数据库映像
我假设它与数据库连接有关?
如何给文件提供路径?
看起来您Products在数据库中没有表格。
[Table("Product")]如果尚未发生,则可能有必要使用Model上的表映射属性()指定其他表名,或者在数据库上开始迁移/初始化。"Invalid object name 'Products'."直接从sql服务器收到错误()| 归档时间: |
|
| 查看次数: |
4765 次 |
| 最近记录: |