Complete C# Tutorial

Models in .Net Core — Simple Guide for Beginners

Models are simply classes that represent data. In simple terms, Models hold the data your app uses. Let’s start with an easy view so you feel good and ready. Don’t worry if this looks new — I’ll break it down step by step. 😄

💡 Super-simple explanation

In other words, a Model is a C# class. Simply put, it describes data. For example, think of a Book class. What this means is the class will have properties like Title, Author, and Price. Let me break it down with a tiny example.

🌍 Real-world scenario — think of a library

Imagine you run a small library. You need to store book details. Therefore, you create a Book model. As a result, your app can add books, list books, or update books. That’s why models are useful.

🧩 Basic Model — Code Example (Plain C#)

				
					// Models/Book.cs
public class Book
{
    public int Id { get; set; }          // unique id
    public string Title { get; set; }    // book title
    public string Author { get; set; }   // author name
    public decimal Price { get; set; }   // price in INR
}
				
			

Explanation:

  • Id is an integer key.
  • Title and Author are strings.
  • Price is a decimal.
    Now let’s use this model in a tiny console demo.

🔧 Small Working Program — Console Demo

				
					// Program.cs (Console App)
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        var books = new List<Book>
        {
            new Book { Id = 1, Title = "C# Basics", Author = "Anita", Price = 299.50M },
            new Book { Id = 2, Title = "ASP.NET Core Guide", Author = "Ravi", Price = 399.00M }
        };

        Console.WriteLine("Books in library:");
        foreach (var b in books)
        {
            Console.WriteLine($"- {b.Id}: {b.Title} by {b.Author} — ₹{b.Price}");
        }
    }
}
				
			

Output:

				
					Books in library:
- 1: C# Basics by Anita — ₹299.5
- 2: ASP.NET Core Guide by Ravi — ₹399.0
				
			

As you can see, using the model is easy. Next, let’s add simple validation.

✅ Adding Validation (DataAnnotations)

				
					using System.ComponentModel.DataAnnotations;

public class Book
{
    public int Id { get; set; }

    [Required(ErrorMessage = "Title is required")]
    public string Title { get; set; }

    public string Author { get; set; }

    [Range(0.01, 10000, ErrorMessage = "Price must be positive")]
    public decimal Price { get; set; }
}
				
			

Now, when you accept input in a form, the framework can check these rules. Important to remember: validation helps stop bad data early.

🌐 Models in ASP.NET Core MVC (Simple idea)

In ASP.NET Core MVC, Models are used by Controllers and Views. For example, a Controller action returns a Book (or list). Then the View shows the data.

For example:

				
					public IActionResult Details(int id)
{
    var book = _db.Books.Find(id); // _db is your DbContext
    return View(book);
}
				
			

Here, the book model travels from database to controller to view. That’s why models are central.

🛠 EF Core Example (Tiny snapshot)

First of all, install EF Core packages. Then define a DbContext:

				
					public class LibraryContext : DbContext
{
    public DbSet<Book> Books { get; set; }

    // OnConfig in Startup or Program: options.UseSqlite("Data Source=lib.db");
}
				
			

Next, add and read:

				
					using (var db = new LibraryContext())
{
    db.Books.Add(new Book { Title = "New C# Book", Author = "Me", Price = 199.99M });
    db.SaveChanges();

    var count = db.Books.Count();
    Console.WriteLine($"Total books: {count}");
}
				
			

Output might be:

				
					Total books: 3
				
			

Also keep in mind: migrations update the DB schema.

💬 Learner struggles & friendly guidance

Many beginners worry about where to put models. Don’t stress. Put domain entities in Models/ folder. On top of that, use ViewModels/ for UI-specific classes. If you feel confused, take small steps. Try one model, then one controller action, and finally the view. You’ll get there!

👉 Next what?

This tutorial covers Models in .Net Core clearly. Furthermore, you saw code examples and a tiny EF Core demo so that .Net Core Models Example makes sense in practice.

👉 In the next chapter, you will learn Controllers in ASP.Net Core (Complete Guide).

Leave a Comment

19 − 14 =

Share this Doc

Models – Handling Data and Logic

Or copy link