Planet model, started adding CSVHelper

This commit is contained in:
Torben Nehmer 2024-07-19 19:08:37 +02:00
parent 469506d704
commit 1abf67fdb3
No known key found for this signature in database
6 changed files with 162 additions and 3 deletions

3
.gitignore vendored
View File

@ -482,3 +482,6 @@ $RECYCLE.BIN/
# Vim temporary swap files
*.swp
# Drop sqlite test files
*.sqlite

View File

@ -0,0 +1,46 @@
using System.ComponentModel.DataAnnotations;
using CSV = CsvHelper.Configuration.Attributes;
namespace net.nehmer.sa.model;
public class Planet
{
[Key, CSV.Index(1)]
public required string Name { get; set; }
public string? Owner { get; set; }
public string? StarbaseType { get; set; }
public int ReportAge { get; set; } = 0;
public int? Population { get; set; }
public int? Value { get; set; }
public int? Mines { get; set; }
public int? Factories { get; set; }
public decimal? DefPercent { get; set; }
public int? SurfaceIronium { get; set; }
public int? SurfaceBoranium { get; set; }
public int? SurfaceGermanium { get; set; }
public int? MRIronium { get; set; }
public int? MRBoranium { get; set; }
public int? MRGermanium { get; set; }
public int? MCIronium { get; set; }
public int? MCBoranium { get; set; }
public int? MCGErmanium { get; set; }
public int? IroniumMC { get; set; }
public int? Resources { get; set; }
public decimal? Gravity { get; set; }
public decimal? Temperature { get; set; }
public decimal? Radiation { get; set; }
public decimal? GravityOrig { get; set; }
public decimal? TemperatureOrig { get; set; }
public decimal? RadiationOrig { get; set; }
public int? MaxTerraforming { get; set; }
public int? CapacityPercent { get; set; }
public int? ScanRange { get; set; }
public int? PenScanRange { get; set; }
public int? Driver { get; set; }
public int? DriverWarp { get; set; }
public string? RouteTarget { get; set; }
public int? GateRange { get; set; }
public int? GateMass { get; set; }
public int? PctDamage { get; set; }
}

View File

@ -0,0 +1,31 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Numerics;
namespace net.nehmer.sa.model;
public class StarsDatabase(string DbPath) : DbContext
{
#region Configuration and Construction
public string DbPath { get; } = DbPath;
/// <summary>
/// Configure for SQLite and set the DB Path given.
/// </summary>
/// <param name="options">Options to update</param>
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlite($"Data Source={DbPath}");
#endregion
#region Entities
/// <summary>
/// List of all Planets read.
/// </summary>
public DbSet<Planet> Planets { get; set; }
#endregion
}

View File

@ -1,2 +1,37 @@
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
using System;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using net.nehmer.sa.model;
using var db = new StarsDatabase("stars.sqlite");
db.Database.EnsureDeleted();
db.Database.EnsureCreated();
// Note: This sample requires the database to be created before running.
Console.WriteLine($"Database path: {db.DbPath}.");
/*
// Create
Console.WriteLine("Inserting a new blog");
db.Add(new Blog { Url = "http://blogs.msdn.com/adonet" });
db.SaveChanges();
// Read
Console.WriteLine("Querying for a blog");
var blog = db.Blogs
.OrderBy(b => b.BlogId)
.First();
// Update
Console.WriteLine("Updating the blog and adding a post");
blog.Url = "https://devblogs.microsoft.com/dotnet";
blog.Posts.Add(
new Post { Title = "Hello World", Content = "I wrote an app using EF Core!" });
db.SaveChanges();
// Delete
// Console.WriteLine("Delete the blog");
// db.Remove(blog);
// db.SaveChanges();
*/

View File

@ -3,9 +3,14 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Stars_Assistant</RootNamespace>
<RootNamespace>net.nehmer.sa</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CsvHelper" Version="33.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.7" />
</ItemGroup>
</Project>

39
Stars Assistant/demo.cs Normal file
View File

@ -0,0 +1,39 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
public class BloggingContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
public string DbPath { get; }
public BloggingContext()
{
DbPath = "./test.sqlite";
}
// The following configures EF to create a Sqlite database file in the
// special "local" folder for your platform.
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlite($"Data Source={DbPath}");
}
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
public List<Post> Posts { get; } = new();
}
public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public int BlogId { get; set; }
public Blog Blog { get; set; }
}