Learning ASP.NET Core: MVC setup and creating the first models

I started out the other day by creating a new project with the standard MVC template.

I’ve decided to focus on functionality before doing layout, so I started by adding support for PostGreSQL through Entity framework core.

NOTE: For people trying to find information on ASP.NET Core. If you look at tutorials using version 1.0, they are referring to project.json. That file is no longer used, and all the package information must be placed in the .csproj file. To edit the .csproj file. Right clock your project in solution explorer.

The relevant parts of my  .csproj file looks like this right now.

 

<ItemGroup>
<PackageReference Include=”Microsoft.ApplicationInsights.AspNetCore” Version=”2.0.0″ />
<PackageReference Include=”Microsoft.AspNetCore” Version=”1.1.2″ />
<PackageReference Include=”Microsoft.AspNetCore.Identity.EntityFrameworkCore” Version=”1.1.2″ />
<PackageReference Include=”Microsoft.AspNetCore.Mvc” Version=”1.1.3″ />
<PackageReference Include=”Microsoft.AspNetCore.StaticFiles” Version=”1.1.2″ />
<PackageReference Include=”Microsoft.Extensions.Logging.Debug” Version=”1.1.2″ />
<PackageReference Include=”Microsoft.VisualStudio.Web.BrowserLink” Version=”1.1.2″ />
<PackageReference Include=”Microsoft.EntityFrameworkCore.Tools” Version=”1.1.1″ />
<PackageReference Include=”Microsoft.AspNetCore.Mvc.Razor” Version=”1.1.3″ />
<PackageReference Include=”Microsoft.AspNetCore.WebUtilities” Version=”1.1.2″ />
<PackageReference Include=”Microsoft.EntityFrameworkCore.Design” Version=”1.1.2″ />
<PackageReference Include=”Microsoft.Extensions.Logging.Debug” Version=”1.1.2″ />
<PackageReference Include=”Microsoft.VisualStudio.Web.CodeGeneration.Design” Version=”1.1.1″ />
<PackageReference Include=”Npgsql.EntityFrameworkCore.PostgreSQL” Version=”1.1.0″ />
<PackageReference Include=”Npgsql.EntityFrameworkCore.PostgreSQL.Design” Version=”1.1.0″ />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include=”Microsoft.VisualStudio.Web.CodeGeneration.Tools” Version=”1.0.1″ />
<DotNetCliToolReference Include=”Microsoft.EntityFrameworkCore.Tools.DotNet” Version=”1.0.0″ />
</ItemGroup>

My editor is probably going to screw this code up, but at least you can see the packages.

Note that the Microsoft.EntityFrameworkCore.Tools.DotNet package has to be added manually. The newest version is not on NuGet and from what I can tell it won’t be added. You’ll need this if you want to run the dotnet ef stuff from the command line (see below).

I also added the identity packages to handle user authentication, but haven’t used it yet.

Since I don’t have a database yet, I decided to take a code first approach and build my tables from models. I won’t list all my entities here, but here’s the frist version of my article (post) class to use as an example.

 


public class Article
{
public int Id { get; set; }
public string ArticleSlug { get; set; }
public string ArticleTitle { get; set; }
public string ArticleKeywords { get; set; }
public string ArticleDescription { get; set; }
public string ArticleContent { get; set; }
public bool AllowComments { get; set; }
public DateTime LastUpdated { get; set; }
public DateTime PostedDate { get; set; }
public int CategoryId { get; set; }
public int ArticleType { get; set; }

}

Simple enough. Doing it this way, I could set up a bunch of entities faster than I would do it using a tool to edit the database directly.

When I had my models, I created my database context class.

I basically copied and pasted one from an example and made the changes I needed to make it work.

If you want a quick setup guide from someone that probably knows what he’s doing. check this link.

Once all this wa done, it was time to make the first migration. More on this in the next post.

 

 

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *