using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
namespace EFGetStarted
{
public class Context : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlite(@"Data Source=blogging.db");
protected override void OnModelCreating(ModelBuilder modelBuilder) { }
public DbSet<GarageDoorModel> GarageDoorModels { get; set; }
public DbSet<Section> Sections { get; set; }
}
public class GarageDoorModel
{
public int GarageDoorModelId { get; set; }
public string ModelName { get; set; }
public ICollection<Section> Sections { get; set; }
}
public class Section
{
public int SectionId { get; set; }
public string SectionName { get; set; }
public string Key { get; set; }
public ICollection<GarageDoorModel> GarageDoorModels { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using EFGetStarted;
namespace SchoolCourse
{
internal class Program
{
private static void Main()
{
using var db = new Context();
Console.WriteLine("Inserting new records");
var modelName = "X";
var sections = new List<Section> {
new Section
{
SectionName = "Engine",
Key = "front_engine"
},
};
db.Add(new GarageDoorModel {ModelName = modelName});
db.SaveChanges();
var gdm = db.GarageDoorModels.First(g => g.ModelName == modelName);
gdm.Sections = sections;
db.SaveChanges();
var new_gdm = db.GarageDoorModels.First(g => g.ModelName == modelName);
Console.WriteLine(new_gdm.Sections);
foreach (var VARIABLE in new_gdm.Sections)
{
Console.WriteLine(VARIABLE.Key);
}
}
}
}
Project 'SchoolCourse' has the following package references
[net5.0]:
Top-level Package Requested Resolved
> Microsoft.EntityFrameworkCore.Design 5.0.5 5.0.5
> Microsoft.EntityFrameworkCore.Sqlite 5.0.5 5.0.5
> Microsoft.EntityFrameworkCore.SqlServer 5.0.5 5.0.5