using System;
using System.Configuration;
using System.Linq;
using Microsoft.EntityFrameworkCore;
 
namespace DatabaseExample
{
    // Define your model (table)
    public class Person
    {
        public int PersonId { get; set; } // Primary key
        public string Name { get; set; }
        public int Age { get; set; }
    }
 
    // Define your database context
    public class MyDbContext : DbContext
    {
        public DbSet<Person> People { get; set; } // Represents the "People" table
 
        // Configure the database connection
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            // Read the connection string from app.config
            string connectionString = ConfigurationManager.ConnectionStrings["MyDatabaseConnection"].ConnectionString;
            optionsBuilder.UseSqlServer(connectionString);
        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            using (var context = new MyDbContext())
            {
                // Ensure the database is created
                context.Database.EnsureCreated();
 
                // Add a sample person if the table is empty
                if (!context.People.Any())
                {
                    context.People.Add(new Person { Name = "Alice", Age = 25 });
                    context.SaveChanges();
                }
 
                // Retrieve only the Age of a specific person
                string personName = "Alice";
                int age = context.People
                    .Where(p => p.Name == personName) // Filter by name
                    .Select(p => p.Age)              // Select only the Age property
                    .FirstOrDefault();               // Get the first result (or default if not found)
 
                // Display the retrieved age
                if (age != 0)
                {
                    Console.WriteLine($"{personName}'s age is: {age}");
                }
                else
                {
                    Console.WriteLine($"No person named '{personName}' found.");
                }
            }
        }
    }
}