Written by Mark Pringle | Last Updated on Saturday, November 05, 2022

MVC ASP.NET Version: 6.0 General Information

Many full-stack developers like to create database tables and ASP.NET MVC models manually. If you are one of these developers, there are a few things you need to keep in mind if you use Entity Framework to scaffold your controllers and CRUD views.

One of the most important things to remember when creating your database tables is to keep the column names descriptive while not using characters like dashes, spaces, or underscores in the column names.

If your database table column names are comprised of two or three words, you can use a title case format (capitalize the first letter of each word) to make the table names more readable. Entity framework does not like mapping models to database tables if you use non-alpha-numeric characters.

Similarly, as you create a class, ensure that the property names within the class mirror the names of the database table columns. For example, compare the column names in the image below to the associated class property names below that image.

Database Columns for Articles Table

    public partial class TableArticles
    {
        [Key]
        public int Id { get; set; }
        public string ContributorId { get; set; } = null!;
        public string Title { get; set; } = null!;
        public string MetaTitle { get; set; } = null!;
        
        [DataType(DataType.MultilineText)]
        public string MetaDescription { get; set; } = null!;

        [DataType(DataType.MultilineText)]
        public string Content { get; set; } = null!;
        public string? Image { get; set; }
        public string? CategoryId { get; set; }
        public string? InformationTypeId { get; set; }
        public decimal? AspnetVersionId { get; set; }
        public string? Keywords { get; set; }
        public DateTime DatePosted { get; set; }
        public DateTime DateUpdated { get; set; }
    }

In comparing the class property names with the table column names above, you no doubt noticed that they are the same. While the case is different in the class, that does not hinder the Entity Framework mappings.