0

I have created an ASP.NET Core project and a separate project that id the DAL (Data Access Layer). In the DAL project, I have a context that is very simple.

public class AtfContext : DbContext
{
    public DbSet<Address> Addresses { get; set; }
    public DbSet<EmployeeType> EmployeeTypes { get; set; }
    public DbSet<Employee> Employees { get; set; }
}

I will define IRepository and Repository here too.

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;
    }
}

I don't think you need the entities.

So I started building a vary simple controller

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();
    }
}

The index view looks like:

@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>

Probably should show project.json too.

 {
  "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%" ]
  }
}

Okay, so now to explain what is wrong. I set up a test project that is a console all and ran Console.WriteLine(repository.FindAll().Count()) and I got a count of 2. In the asp.net core app I get 0 records that show up.

Can anyone explain why?

4
  • Have you tried to pass connections string directly to datacontext constructor? Commented Sep 9, 2016 at 15:39
  • no. how would i get that? i looked in the DAL config file for that but didnt see one there Commented Sep 9, 2016 at 15:42
  • You should be making use of Core's Dependency Injection model, which will naturally offer a point to inject the connection string. see here Commented Sep 9, 2016 at 16:06
  • Have you tried to use intellitrace to see if the query is called or any error us throw? Commented Sep 9, 2016 at 20:52

1 Answer 1

2

You'll need to configure the connection string in your Startup, and use dependency injection to pull in an instance of the db context. See the official docs:

https://docs.efproject.net/en/latest/platforms/aspnetcore/new-db.html#register-your-context-with-dependency-injection

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.