0% found this document useful (0 votes)
7 views4 pages

.NET Customer CRUD App with SQL Server

The document outlines a .NET application that connects to a SQL Server database to manage customer data. It includes CRUD (Create, Read, Update, Delete) endpoints for customer records and utilizes Entity Framework Core for database operations. The application also sets up Swagger for API documentation and auto-creates the database on startup.

Uploaded by

carewelloman
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

.NET Customer CRUD App with SQL Server

The document outlines a .NET application that connects to a SQL Server database to manage customer data. It includes CRUD (Create, Read, Update, Delete) endpoints for customer records and utilizes Entity Framework Core for database operations. The application also sets up Swagger for API documentation and auto-creates the database on startup.

Uploaded by

carewelloman
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Customer Application using .

NET and SQL Server

using [Link];

var builder = [Link](args);

// SQL Server connection

[Link]<AppDbContext>(options =>

[Link](

"Server=.;Database=CustomerDB;Trusted_Connection=True;TrustServerCertificate=Tru
e;"

));

[Link]();

[Link]();

var app = [Link]();

// Auto-create database

using (var scope = [Link]())

var db = [Link]<AppDbContext>();

[Link]();

[Link]();

[Link]();

// --------- CRUD ENDPOINTS ---------


Customer Application using .NET and SQL Server

// GET all customers

[Link]("/customers", async (AppDbContext db) =>

await [Link]());

// GET customer by id

[Link]("/customers/{id:int}", async (int id, AppDbContext db) =>

await [Link](id)

is Customer c ? [Link](c) : [Link]());

// CREATE customer

[Link]("/customers", async (Customer customer, AppDbContext db) =>

[Link](customer);

await [Link]();

return [Link]($"/customers/{[Link]}", customer);

});

// UPDATE customer

[Link]("/customers/{id:int}", async (int id, Customer input, AppDbContext db) =>

var customer = await [Link](id);

if (customer is null) return [Link]();

[Link] = [Link];

[Link] = [Link];

[Link] = [Link];

[Link] = [Link];
Customer Application using .NET and SQL Server

await [Link]();

return [Link]();

});

// DELETE customer

[Link]("/customers/{id:int}", async (int id, AppDbContext db) =>

var customer = await [Link](id);

if (customer is null) return [Link]();

[Link](customer);

await [Link]();

return [Link]();

});

[Link]();

// ---------- MODELS & DB CONTEXT ----------

class Customer

public int CustomerId { get; set; }

public string FullName { get; set; }

public string? Email { get; set; }

public string? Phone { get; set; }

public string? Address { get; set; }

public DateTime CreatedDate { get; set; } = [Link];

}
Customer Application using .NET and SQL Server

class AppDbContext : DbContext

public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }

public DbSet<Customer> Customers => Set<Customer>();

You might also like