Skip to content
This repository has been archived by the owner on Jul 13, 2024. It is now read-only.

Commit

Permalink
Merge pull request #6 from Star-Academy/EFCore
Browse files Browse the repository at this point in the history
complete EFCore practice
  • Loading branch information
Mahdi-Shah authored Feb 19, 2024
2 parents 53e26cb + 8d627ed commit fdc0b25
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 1 deletion.
18 changes: 18 additions & 0 deletions EFCore/EFCore.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.1" />
</ItemGroup>

</Project>
34 changes: 34 additions & 0 deletions EFCore/Model.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Microsoft.EntityFrameworkCore;

public class BloggingContext : DbContext
{
public DbSet<Blog>? Blogs { get; set; }
public DbSet<Post>? Posts { get; set; }


// 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.UseNpgsql("User ID=postgres;Password=1234;" +
"Host=localhost;Port=5432;Database=staracademy;" +
"Connection Lifetime=0;");
}

public class Blog
{
public string BlogId { get; set; }
public string? Url { get; set; }

public List<Post>? Posts { get; } = new();
}

public class Post
{

public string PostId { get; set; }
public string? Title { get; set; }
public string? Content { get; set; }

public int BlogId { get; set; }
public Blog? Blog { get; set; }
}
35 changes: 35 additions & 0 deletions EFCore/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using var db = new BloggingContext();

// Create
Console.WriteLine("Inserting a new blog");
Blog firstBlog = new Blog
{
BlogId = Guid.NewGuid().ToString(),
Url = "http://blogs.msdn.com/adonet"
};

db.Add(firstBlog);
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";
Post post = new Post
{
Title = "Hello World",
Content = "I wrote an app using EF Core!",
PostId = Guid.NewGuid().ToString(),
};
blog.Posts!.Add(post);
db.SaveChanges();

// Delete
Console.WriteLine("Delete the blog");
db.Remove(blog);
db.SaveChanges();
2 changes: 1 addition & 1 deletion Folder.DotSettings.user
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ASimpleCalculator_002Ecs_002Fl_003AUnitTest_003FSimpleCalculator_003FSimpleCalculator_002EBusiness_003FSimpleCalculator_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>

<s:String x:Key="/Default/CodeInspection/PencilsConfiguration/ActualSeverity/@EntryValue">INFO</s:String>
<s:String x:Key="/Default/Environment/Highlighting/HighlightingSourceSnapshotLocation/@EntryValue">C:\Users\m.shahmoradi\AppData\Local\JetBrains\Rider2023.3\resharper-host\temp\Rider\vAny\CoverageData\_Fall1402-MahdiShahMoradi.19\Snapshot\snapshot.utdcvr</s:String>


</wpf:ResourceDictionary>

0 comments on commit fdc0b25

Please sign in to comment.