diff --git a/tp1/Library/DbContextLib/LibraryContext.cs b/tp1/Library/DbContextLib/LibraryContext.cs new file mode 100644 index 0000000..5918c9d --- /dev/null +++ b/tp1/Library/DbContextLib/LibraryContext.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using Library.Entities; + + +namespace Library.DbContextLib +{ + public class LibraryContext : DbContext + { + public DbSet BooksSet { get; set; } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + optionsBuilder.UseSqlite($"Data Source=tp1.Books.db"); + } + + } +} diff --git a/tp1/Library/Entities/BookEntity.cs b/tp1/Library/Entities/BookEntity.cs new file mode 100644 index 0000000..4ecea42 --- /dev/null +++ b/tp1/Library/Entities/BookEntity.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Library.Entities +{ + public class BookEntity + { + public int ID { get; set; } + public string Title { get; set; } + public string Author { get; set; } + public string Isbn { get; set; } + + public BookEntity(string title, string author, string isbn) { + Title = title; + Author = author; + Isbn = isbn; + } + } +} diff --git a/tp1/Library/Library.csproj b/tp1/Library/Library.csproj new file mode 100644 index 0000000..0c79874 --- /dev/null +++ b/tp1/Library/Library.csproj @@ -0,0 +1,22 @@ + + + + net8.0 + enable + enable + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + diff --git a/tp1/Library/StubbedContextLib/StubbedContext.cs b/tp1/Library/StubbedContextLib/StubbedContext.cs new file mode 100644 index 0000000..4474e2b --- /dev/null +++ b/tp1/Library/StubbedContextLib/StubbedContext.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Library.DbContextLib; +using Library.Entities; +using Microsoft.EntityFrameworkCore; + +namespace Library.StubbedContextLib +{ + public class StubbedContext : DbContext + { + public List Books { get; set; } + + public StubbedContext() { + Books.Add(new BookEntity("test", "test", "test")); + Books.Add(new BookEntity("test2", "test2", "test2")); + } + + } +} diff --git a/tp1/TestStub/Program.cs b/tp1/TestStub/Program.cs new file mode 100644 index 0000000..8a17720 --- /dev/null +++ b/tp1/TestStub/Program.cs @@ -0,0 +1,40 @@ +using Library; +using Library.DbContextLib; +using Library.Entities; +using Microsoft.Extensions.Options; + +// See https://aka.ms/new-console-template for more information +Console.WriteLine("Hello, World!"); + +using (var context = new LibraryContext()) +{ + BookEntity chewie = new BookEntity("B3", "test", "test2"); + BookEntity yoda = new BookEntity ("B4", "test", "test2"); + BookEntity ewok = new BookEntity ("C5", "test", "test3"); + + context.BooksSet.Add(chewie); + context.BooksSet.Add(yoda); + context.BooksSet.Add(ewok); + context.SaveChanges(); +} + + +using (var context = new LibraryContext()) +{ + foreach (var n in context.BooksSet) + { + Console.WriteLine($"{n.ID} - {n.Title}"); + } + + context.SaveChanges(); + +} + +using (var context = new LibraryContext()) +{ + var eBooks = context.BooksSet.Where(b => b.Title.StartsWith("B")).First(); + Console.WriteLine($"{eBooks.Title} (born in {eBooks.Author})"); + + eBooks.Title = "Wicket"; + context.SaveChanges(); +} \ No newline at end of file diff --git a/tp1/TestStub/TestStub.csproj b/tp1/TestStub/TestStub.csproj new file mode 100644 index 0000000..546f891 --- /dev/null +++ b/tp1/TestStub/TestStub.csproj @@ -0,0 +1,28 @@ + + + + Exe + net8.0 + enable + enable + $(MSBuildProjectDirectory) + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + diff --git a/tp1/tp1.sln b/tp1/tp1.sln index eed7fb5..a0c64a8 100644 --- a/tp1/tp1.sln +++ b/tp1/tp1.sln @@ -3,13 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.8.34330.188 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestStubEF", "tp1\TestStubEF.csproj", "{2097BC86-5063-42A3-83E2-1004732AC629}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestStub", "TestStub\TestStub.csproj", "{8960D74C-259D-4779-80B6-789BA257D2B2}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Entities", "Entities\Entities.csproj", "{19774C29-80C4-4B8C-A7D9-8F42576C4D1C}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DbContextLib", "DbContextLib\DbContextLib.csproj", "{785FFEAF-A688-470C-AB63-5F83219177BC}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StubbedContextLib", "StubbedContextLib\StubbedContextLib.csproj", "{56AB577F-DC4D-4DDE-81ED-6376FB52D5EA}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Library", "Library\Library.csproj", "{CDD5A87A-187A-4BC1-9E23-5559A2211B55}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -17,22 +13,14 @@ Global Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {2097BC86-5063-42A3-83E2-1004732AC629}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {2097BC86-5063-42A3-83E2-1004732AC629}.Debug|Any CPU.Build.0 = Debug|Any CPU - {2097BC86-5063-42A3-83E2-1004732AC629}.Release|Any CPU.ActiveCfg = Release|Any CPU - {2097BC86-5063-42A3-83E2-1004732AC629}.Release|Any CPU.Build.0 = Release|Any CPU - {19774C29-80C4-4B8C-A7D9-8F42576C4D1C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {19774C29-80C4-4B8C-A7D9-8F42576C4D1C}.Debug|Any CPU.Build.0 = Debug|Any CPU - {19774C29-80C4-4B8C-A7D9-8F42576C4D1C}.Release|Any CPU.ActiveCfg = Release|Any CPU - {19774C29-80C4-4B8C-A7D9-8F42576C4D1C}.Release|Any CPU.Build.0 = Release|Any CPU - {785FFEAF-A688-470C-AB63-5F83219177BC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {785FFEAF-A688-470C-AB63-5F83219177BC}.Debug|Any CPU.Build.0 = Debug|Any CPU - {785FFEAF-A688-470C-AB63-5F83219177BC}.Release|Any CPU.ActiveCfg = Release|Any CPU - {785FFEAF-A688-470C-AB63-5F83219177BC}.Release|Any CPU.Build.0 = Release|Any CPU - {56AB577F-DC4D-4DDE-81ED-6376FB52D5EA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {56AB577F-DC4D-4DDE-81ED-6376FB52D5EA}.Debug|Any CPU.Build.0 = Debug|Any CPU - {56AB577F-DC4D-4DDE-81ED-6376FB52D5EA}.Release|Any CPU.ActiveCfg = Release|Any CPU - {56AB577F-DC4D-4DDE-81ED-6376FB52D5EA}.Release|Any CPU.Build.0 = Release|Any CPU + {8960D74C-259D-4779-80B6-789BA257D2B2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8960D74C-259D-4779-80B6-789BA257D2B2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8960D74C-259D-4779-80B6-789BA257D2B2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8960D74C-259D-4779-80B6-789BA257D2B2}.Release|Any CPU.Build.0 = Release|Any CPU + {CDD5A87A-187A-4BC1-9E23-5559A2211B55}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CDD5A87A-187A-4BC1-9E23-5559A2211B55}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CDD5A87A-187A-4BC1-9E23-5559A2211B55}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CDD5A87A-187A-4BC1-9E23-5559A2211B55}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/tp1/tp1/Program.cs b/tp1/tp1/Program.cs index 83fa4f4..bb851ad 100644 --- a/tp1/tp1/Program.cs +++ b/tp1/tp1/Program.cs @@ -1,2 +1,4 @@ -// See https://aka.ms/new-console-template for more information +using Microsoft.EntityFrameworkCore.Design; + +// See https://aka.ms/new-console-template for more information Console.WriteLine("Hello, World!"); diff --git a/tp1/tp1/TestStubEF.csproj b/tp1/tp1/TestStubEF.csproj index c25f2d9..e4b7805 100644 --- a/tp1/tp1/TestStubEF.csproj +++ b/tp1/tp1/TestStubEF.csproj @@ -19,6 +19,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive +