C# Asp.net核心和实体框架可以';无法连接到SQL Server
我创建了一个ASP.NET核心项目和一个单独的项目,用于标识DAL(数据访问层)。在DAL项目中,我有一个非常简单的上下文C# Asp.net核心和实体框架可以';无法连接到SQL Server,c#,asp.net-mvc,entity-framework,asp.net-core,C#,Asp.net Mvc,Entity Framework,Asp.net Core,我创建了一个ASP.NET核心项目和一个单独的项目,用于标识DAL(数据访问层)。在DAL项目中,我有一个非常简单的上下文 public class AtfContext : DbContext { public DbSet<Address> Addresses { get; set; } public DbSet<EmployeeType> EmployeeTypes { get; set; } public DbSet<Employee&
public class AtfContext : DbContext
{
public DbSet<Address> Addresses { get; set; }
public DbSet<EmployeeType> EmployeeTypes { get; set; }
public DbSet<Employee> Employees { get; set; }
}
我认为你不需要实体
所以我开始构建一个简单的控制器
public class EmployeeController : Controller
{
// GET: /<controller>/
public IActionResult Index()
{
IRepository<Employee> repository = new Repository<Employee>();
var employees = repository.FindAll().ToList();
return View(employees);
}
public IActionResult Add()
{
var model = new Employee();
return View();
}
}
好的,现在来解释什么是错的。我设置了一个测试项目,它是一个控制台,运行了console.WriteLine(repository.FindAll().Count())
,得到了一个2。在asp.net核心应用程序中,我得到0条显示的记录
有人能解释一下原因吗?您需要在启动时配置连接字符串,并使用依赖项注入来拉入db上下文的实例。见官方文件:
您需要在启动时配置连接字符串,并使用依赖项注入来拉入db上下文的实例。见官方文件:
您是否尝试将连接字符串直接传递给datacontext构造函数?否。如何获得该字符串?我查看了DAL配置文件,但没有看到其中的一个。您应该使用Core的依赖项注入模型,它自然会提供一个注入连接字符串的点。您是否尝试使用intellitrace查看查询是否被调用或我们抛出的任何错误?您是否尝试将连接字符串直接传递给datacontext构造函数?否。我将如何获得它?我查看了DAL配置文件,但没有看到其中的一个。您应该使用Core的依赖项注入模型,它自然会提供一个注入连接字符串的点。您是否尝试使用intellitrace查看是否调用了查询或是否抛出任何错误?
public class EmployeeController : Controller
{
// GET: /<controller>/
public IActionResult Index()
{
IRepository<Employee> repository = new Repository<Employee>();
var employees = repository.FindAll().ToList();
return View(employees);
}
public IActionResult Add()
{
var model = new Employee();
return View();
}
}
@model IEnumerable<Atf.DataAccess.Models.Employee>
@{
ViewBag.Title = "Home";
}
<div id="main">
<h2>Employees</h2>
</div>
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Phone</th>
<th>Zip</th>
</tr>
@foreach (var employee in @Model)
{
<tr>
<td>@employee.FirstName</td>
<td>@employee.LastName</td>
<td>@employee.MobilePhone</td>
<td>@employee.Address.Zip</td>
</tr>
}
</table>
{
"dependencies": {
"Microsoft.AspNetCore.Diagnostics": "1.0.0",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.AspNetCore.StaticFiles": "1.0.0",
"Microsoft.AspNetCore.Mvc": "1.0.0",
"Microsoft.AspNetCore.Mvc.TagHelpers": "1.0.0",
"EntityFramework": "6.1.3",
"Microsoft.AspNetCore.Razor.Tools": {
"version": "1.0.0-preview2-final",
"type": "build"
},
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.Extensions.Logging": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
"Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0"
},
"tools": {
"BundlerMinifier.Core": "2.0.238",
"Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",
"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
},
"frameworks": {
"net452": {
"dependencies": {
"Atf.DataAccess": {
"target": "project"
}
}
}
},
"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true
},
"publishOptions": {
"include": [
"wwwroot",
"web.config"
]
},
"scripts": {
"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
}
}