-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
67 lines (59 loc) · 1.91 KB
/
Program.cs
File metadata and controls
67 lines (59 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using Microsoft.EntityFrameworkCore;
using System;
namespace ConsoleApp_EntityFramework_CF
{
// Packages To Add
// EntityFrameworkCore
// EntityFrameworkCore.Tools
// EntityFrameworkCore.SqlServer
// Commands to execute in Package Manager Console
// Add-Migration Initial
// Update-Database
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Console App - EntityFramework/CF");
Console.WriteLine("\n\n- Press enter to add a <Car> to DB");
try
{
VehicleContext db = new VehicleContext();
Car newCar = new("Saab", "95 Turbo", 2002, 210);
db.Cars.Add(newCar);
db.SaveChanges();
Console.WriteLine("<Car> was added to DB");
}
catch(Exception ex)
{
Console.WriteLine($"Failed to save <Car> to DB\nErr: {ex}");
}
finally
{
Console.ReadLine();
}
}
}
class Car
{
public Car(string brand, string model, int year, int speed)
{
Brand = brand;
Model = model;
Year = year;
Speed = speed;
}
public int Id { get; set; }
public string Brand { get; set; }
public string Model { get; set; }
public int Year { get; set; }
public int Speed { get; set; }
}
class VehicleContext : DbContext
{
public DbSet<Car> Cars { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=Vehicle_EF_CF;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
}
}
}