私はASP.NETコアプロジェクトと、DAL(データアクセスレイヤ)を識別する別のプロジェクトを作成しました。 DALプロジェクトでは、私は非常にシンプルなコンテキストを持っています。Asp.netコアとEntity FrameworkがSQL Serverに接続できません
public class AtfContext : DbContext
{
public DbSet<Address> Addresses { get; set; }
public DbSet<EmployeeType> EmployeeTypes { get; set; }
public DbSet<Employee> Employees { get; set; }
}
私もここにIRepository
とRepository
を定義します。
public interface IRepository<T> where T : class, IEntity
{
void Add(T entity);
T Find(int id);
IEnumerable<T> FindAll();
void Delete(int id);
void Delete(T entity);
int Save();
}
public class Repository<T> : IRepository<T> where T : class, IEntity
{
private AtfContext _context;
public Repository()
{
_context = new AtfContext();
}
public void Add(T entity)
{
_context.Set<T>().Add(entity);
}
public void Delete(T entity)
{
_context.Set<T>().Remove(entity);
}
public void Delete(int id)
{
var entity = Find(id);
Delete(entity);
}
public T Find(int id)
{
return _context.Set<T>().FirstOrDefault(x => x.Id == id);
}
public IEnumerable<T> FindAll()
{
return _context.Set<T>();
}
public int Save()
{
try
{
return _context.SaveChanges();
}
catch (Exception)
{
}
return 0;
}
}
エンティティは必要ありません。
だから私は
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();
}
}
を変える簡単なコントローラを構築し始めたインデックスビューは次のようになります。おそらく、あまりにもproject.json
を示すべき
@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%" ]
}
}
これで間違っていることを説明します。私はすべてのコンソールであるテストプロジェクトをセットアップし、Console.WriteLine(repository.FindAll().Count())
を実行し、2のカウントを得ました。asp.netコアアプリケーションで、私は表示される0のレコードを取得します。
誰でも理由を説明できますか?
あなたはDataContextのコンストラクタに直接接続文字列を渡すためにしたことがありますか? – tym32167
番号。どのように私はそれを得るだろうか?私はDAL設定ファイルを見ましたが、そこには表示されませんでした。 – JamTay317
コアのDependency Injectionモデルを使用する必要があります。これは当然、接続文字列を挿入するポイントを提供します。 [こちらを参照](https://docs.efproject.net/en/latest/miscellaneous/connection-strings.html#asp-net-core) –