65 lines
1.8 KiB
C#

using MySql.Data.MySqlClient;
using System;
namespace MontoyaTech.MySqlPlus.Example
{
public class Program
{
[MySqlRow("cars")]
[MySqlRowIndex("model_year", "model", "year")]
[MySqlRowIndex("year", "year")]
public class Car
{
[MySqlColumn(Name = "id", PrimaryKey = true, AutoIncrement = true)]
public ulong Id = 0;
[MySqlColumn("make")]
public string Make = null;
[MySqlColumn("model", Type = "VARCHAR(255)")]
public string Model = "Unknown";
[MySqlColumn("year")]
public uint Year = 0;
[MySqlColumn("compact")]
public bool? Compact = null;
[MySqlColumn("dateCreated", typeof(DateTimeToUnixConverter), DefaultValue = 0)]
public DateTime DateCreated = DateTime.UtcNow;
}
public static void Main(string[] args)
{
var session = new MySqlSession("");
if (session.TableExists<Car>())
session.DeleteTable<Car>();
session.CreateTable<Car>();
session.DeleteAll<Car>();
session.Insert(new Car() { Make = "Chevy", Model = "Camaro", Year = 2011, Compact = true });
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}, Compact: {car.Compact}, 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();
}
}
}