using System;
using Microsoft.EntityFrameworkCore;
 
namespace DatabaseExample
{
    // Step 1: Define your model (table)
    public class Person
    {
        public int PersonId { get; set; } // Primary key
        public string Name { get; set; }
        public int Age { get; set; }
    }
 
    // Step 2: 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)
        {
            // Use SQL Server as the database provider
            optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=MyDatabase;Trusted_Connection=True;");
        }
 
        // Optional: Configure the model (e.g., table name, constraints)
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Person>().ToTable("People"); // Explicitly set the table name
        }
    }
 
    // Step 3: Programmatically create the database and perform operations
    class Program
    {
        static void Main(string[] args)
        {
            // Ensure the database is created
            using (var context = new MyDbContext())
            {
                // Create the database if it doesn't exist
                context.Database.EnsureCreated();
 
                // Step 4: Add a new person to the table
                var person = new Person { Name = "Alice", Age = 25 };
                context.People.Add(person);
                context.SaveChanges();
 
                Console.WriteLine("Person added successfully.");
 
                // Step 5: Update a person's age
                var personToUpdate = context.People.FirstOrDefault(p => p.Name == "Alice");
                if (personToUpdate != null)
                {
                    personToUpdate.Age = 26;
                    context.SaveChanges();
                    Console.WriteLine("Person updated successfully.");
                }
 
                // Step 6: Display all people in the table
                Console.WriteLine("People in the database:");
                foreach (var p in context.People)
                {
                    Console.WriteLine($"ID: {p.PersonId}, Name: {p.Name}, Age: {p.Age}");
                }
            }
            
                // Step 1: 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)
 
                // Step 2: Display the retrieved age
                if (age != 0)
                {
                    Console.WriteLine($"{personName}'s age is: {age}");
                }
                else
                {
                    Console.WriteLine($"No person named '{personName}' found.");
                }
            }
        }
    }
}
        }
    }
}
/*Explanation of the Code
Database Creation:
 
The EnsureCreated() method checks if the database exists. If it doesn't, it creates the database and all the tables defined in the DbContext.
 
Adding Records:
 
A new Person object is created and added to the People table using context.People.Add().
 
The SaveChanges() method commits the changes to the database.
 
Updating Records:
 
The FirstOrDefault() method retrieves a specific record from the table.
 
The record is updated, and SaveChanges() is called to persist the changes.
 
Displaying Records:
 
All records in the People table are retrieved and displayed using a foreach loop.
 
Key Points
No SQL Required: You don't need to write any SQL queries. Entity Framework Core generates the SQL for you.
 
No Bash or Command-Line Tools: Everything is done programmatically in C#.
 
Automatic Database Creation: The EnsureCreated() method handles the creation of the database and tables.
 
Flexibility: You can modify the model (e.g., add new properties) and the database schema will be updated automatically when you run the program again.
*/
 

Databaze 2