57 lines
1.6 KiB
C#

using MySql.Data.MySqlClient;
using System;
namespace MontoyaTech.MySqlPlus.Example
{
public class Program
{
[MySqlRow("cars")]
public class Car
{
[MySqlColumn(Id = true, Name = "id", PrimaryKey = true, AutoIncrement = true, Nullable = false)]
public ulong Id = 0;
[MySqlColumn("make")]
public string Make = null;
[MySqlColumn("model", Nullable = false, Type = "VARCHAR(255)")]
public string Model = "Unknown";
[MySqlColumn("year", Nullable = false)]
public uint Year = 0;
[MySqlColumn("dateCreated", typeof(DateTimeToUnixConverter), DefaultValue = 0, Nullable = false)]
public DateTime DateCreated = DateTime.UtcNow;
}
public static void Main(string[] args)
{
var session = new MySqlSession("");
session.CreateTable<Car>();
session.DeleteAll<Car>();
session.Insert(new Car() { Make = "Chevy", Model = "Camaro", Year = 2011 });
session.Insert(new Car() { Make = "GMC", Model = "Sierra", Year = 2000 });
var cars = session.GetAll<Car>();
foreach (var car in cars)
Console.WriteLine($"Make: {car.Make}, Model: {car.Model}, Year: {car.Year}, DateCreated: {car.DateCreated}");
cars[0].Make = "test";
session.Update(cars[0]);
foreach (var car in cars)
session.Delete(car);
session.DeleteTable<Car>();
Console.WriteLine("Done.");
Console.ReadLine();
}
}
}