This repository has been archived by the owner on Jul 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from Star-Academy/EFCore
complete EFCore practice
- Loading branch information
Showing
4 changed files
with
88 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters