Wiløvy
Wiløvy
CC#
Created by Wiløvy on 12/9/2024 in #help
EF 9
Okay, I think I understand how to solve it, thank you very much!
65 replies
CC#
Created by Wiløvy on 12/9/2024 in #help
EF 9
But for example, I'm going to create a blog, I have 2 gRPC projects - UserService - PostService I will have one more project to set up the DB - PrepareDB This PrepareDB will be referenced in the 2 gRPC projects. But how do these 2 gRPC projects communicate with each other? Simply with the Both so that they are grpc clients and servers? Although looking at it, I believe that they do not have to communicate between them. Then I will have my REST api where I will also reference that prepareDB project, and here it will also be a gRPC client. To be able to query the microservices And then transform it to REST endopoint
65 replies
CC#
Created by Wiløvy on 12/9/2024 in #help
EF 9
But where is the part where microservices communicate with each other?
65 replies
CC#
Created by Wiløvy on 12/9/2024 in #help
EF 9
Because these microservices are in gRPC and to connect them to the client (Angular) I will use a REST api.
65 replies
CC#
Created by Wiløvy on 12/9/2024 in #help
EF 9
Oh, and I can't put this in my api rest?
65 replies
CC#
Created by Wiløvy on 12/9/2024 in #help
EF 9
Oh okay, so what I have to do is to remove the models and migrations from each gRPC project and put them in a new one called idk PrepareDB that project is going to take care of accommodating all the tables. Then I can run each microservice without any problem.
65 replies
CC#
Created by Wiløvy on 12/9/2024 in #help
EF 9
This is not yet a job, it is a learning project so as not to arrive at work at 0 in January.
65 replies
CC#
Created by Wiløvy on 12/9/2024 in #help
EF 9
Pff I don't understand anything bro, I'm JS dev but I changed my position and now I have to use C#.
65 replies
CC#
Created by Wiløvy on 12/9/2024 in #help
EF 9
This separate project can be my REST API for the JS client?
65 replies
CC#
Created by Wiløvy on 12/9/2024 in #help
EF 9
OKay, then I have to find a way to use EF globally?
65 replies
CC#
Created by Wiløvy on 12/9/2024 in #help
EF 9
I said the same thing, but I got the price
65 replies
CC#
Created by Wiløvy on 12/9/2024 in #help
EF 9
So my project structure is wrong?
65 replies
CC#
Created by Wiløvy on 12/9/2024 in #help
EF 9
And that's not what EF is good for?
65 replies
CC#
Created by Wiløvy on 12/9/2024 in #help
EF 9
📦 NanoStore
├── 📄 angular-cli.nix
├── 📄 flake.lock
├── 📄 flake.nix
├── 📁 frontend
├── 📁 microservices
│ ├── 📁 PostService
│ │ ├── 📄 appsettings.Development.json
│ │ ├── 📄 appsettings.json
│ │ ├── 📁 bin
│ │ ├── 📁 Migrations
│ │ │ ├── 📄 20241209004606_Post.cs
│ │ │ ├── 📄 20241209004606_Post.Designer.cs
│ │ │ └── 📄 PostContextModelSnapshot.cs
│ │ ├── 📁 Models
│ │ │ ├── 📄 Post.cs
│ │ │ └── 📄 PostContext.cs
│ │ ├── 📁 obj
│ │ ├── 📄 PostService.csproj
│ │ ├── 📄 Program.cs
│ │ ├── 📁 Properties
│ │ └── 📁 Services
│ ├── 📁 Protos
│ │ ├── 📁 Posts
│ │ │ └── 📄 post.proto
│ │ └── 📁 User
│ │ └── 📄 user.proto
│ ├── 📄 startup.sh
│ └── 📁 UserService
│ ├── 📄 appsettings.Development.json
│ ├── 📄 appsettings.json
│ ├── 📁 bin
│ ├── 📁 Migrations
│ │ ├── 📄 20241204055139_User.cs
│ │ ├── 📄 20241204055139_User.Designer.cs
│ │ └── 📄 UserContextModelSnapshot.cs
│ ├── 📁 Models
│ │ ├── 📄 User.cs
│ │ └── 📄 UserContext.cs
│ ├── 📁 obj
│ ├── 📄 Program.cs
│ ├── 📁 Properties
│ ├── 📁 Services
│ └── 📄 UserService.csproj
└── 📄 README.md
📦 NanoStore
├── 📄 angular-cli.nix
├── 📄 flake.lock
├── 📄 flake.nix
├── 📁 frontend
├── 📁 microservices
│ ├── 📁 PostService
│ │ ├── 📄 appsettings.Development.json
│ │ ├── 📄 appsettings.json
│ │ ├── 📁 bin
│ │ ├── 📁 Migrations
│ │ │ ├── 📄 20241209004606_Post.cs
│ │ │ ├── 📄 20241209004606_Post.Designer.cs
│ │ │ └── 📄 PostContextModelSnapshot.cs
│ │ ├── 📁 Models
│ │ │ ├── 📄 Post.cs
│ │ │ └── 📄 PostContext.cs
│ │ ├── 📁 obj
│ │ ├── 📄 PostService.csproj
│ │ ├── 📄 Program.cs
│ │ ├── 📁 Properties
│ │ └── 📁 Services
│ ├── 📁 Protos
│ │ ├── 📁 Posts
│ │ │ └── 📄 post.proto
│ │ └── 📁 User
│ │ └── 📄 user.proto
│ ├── 📄 startup.sh
│ └── 📁 UserService
│ ├── 📄 appsettings.Development.json
│ ├── 📄 appsettings.json
│ ├── 📁 bin
│ ├── 📁 Migrations
│ │ ├── 📄 20241204055139_User.cs
│ │ ├── 📄 20241204055139_User.Designer.cs
│ │ └── 📄 UserContextModelSnapshot.cs
│ ├── 📁 Models
│ │ ├── 📄 User.cs
│ │ └── 📄 UserContext.cs
│ ├── 📁 obj
│ ├── 📄 Program.cs
│ ├── 📁 Properties
│ ├── 📁 Services
│ └── 📄 UserService.csproj
└── 📄 README.md
65 replies
CC#
Created by Wiløvy on 12/9/2024 in #help
EF 9
I don't understand Isn't each microservice an independent project?
65 replies
CC#
Created by Wiløvy on 12/9/2024 in #help
EF 9
The problem I have is that at the moment of generating the migration of Post the migration generates in the same file the User table (this table is already created by the other microservice) and at the moment of executing it, it is broken. Any idea how I can solve it?
65 replies
CC#
Created by Wiløvy on 12/9/2024 in #help
EF 9
<!-- microservices/PostService/PostService.csproj -->
<Project Sdk="Microsoft.NET.Sdk.Web">

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

<ItemGroup>
<Protobuf Include="..\Protos\Posts\post.proto" GrpcServices="Server" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Grpc.AspNetCore" Version="2.57.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\UserService\UserService.csproj" />
</ItemGroup>

</Project>
<!-- microservices/PostService/PostService.csproj -->
<Project Sdk="Microsoft.NET.Sdk.Web">

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

<ItemGroup>
<Protobuf Include="..\Protos\Posts\post.proto" GrpcServices="Server" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Grpc.AspNetCore" Version="2.57.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\UserService\UserService.csproj" />
</ItemGroup>

</Project>
65 replies
CC#
Created by Wiløvy on 12/9/2024 in #help
EF 9
// microservices/PostService/Models/PostContext.cs
using Microsoft.EntityFrameworkCore;
using UserModel.Models;

namespace PostModel.Models
{
public class PostContext : DbContext
{
public PostContext(DbContextOptions<PostContext> options) : base(options)
{
}

public DbSet<Post> Post { get; set; }
public DbSet<User> User { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
}
// microservices/PostService/Models/PostContext.cs
using Microsoft.EntityFrameworkCore;
using UserModel.Models;

namespace PostModel.Models
{
public class PostContext : DbContext
{
public PostContext(DbContextOptions<PostContext> options) : base(options)
{
}

public DbSet<Post> Post { get; set; }
public DbSet<User> User { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
}
// microservices/PostService/Services/ManagePostService.cs
using Grpc.Core;
using PostProtoService;
using PostModel.Models;

namespace PostService.Services;

public class ManagePostService : PostProto.PostProtoBase
{
private readonly ILogger<ManagePostService> _logger;
private readonly PostContext _dbContext;

public ManagePostService(ILogger<ManagePostService> logger, PostContext dbContext)
{
_logger = logger;
_dbContext = dbContext;
}

public override async Task<CreateReply> Create(CreateRequest request, ServerCallContext context)
{

var user = _dbContext.User.FirstOrDefault(u => u.Id == request.OwnerId);

if (user != null) throw new RpcException(new Status(StatusCode.NotFound, "El usuario no existe."));

var newPost = new Post
{
OwnerId = request.OwnerId,
Title = request.Title,
Description = request.Description,
Image = request.Image,
};

_dbContext.Post.Add(newPost);
await _dbContext.SaveChangesAsync();

return new CreateReply
{
OwnerId = request.OwnerId,
Image = request.Image,
Title = request.Title,
Description = request.Description
};
}
}
// microservices/PostService/Services/ManagePostService.cs
using Grpc.Core;
using PostProtoService;
using PostModel.Models;

namespace PostService.Services;

public class ManagePostService : PostProto.PostProtoBase
{
private readonly ILogger<ManagePostService> _logger;
private readonly PostContext _dbContext;

public ManagePostService(ILogger<ManagePostService> logger, PostContext dbContext)
{
_logger = logger;
_dbContext = dbContext;
}

public override async Task<CreateReply> Create(CreateRequest request, ServerCallContext context)
{

var user = _dbContext.User.FirstOrDefault(u => u.Id == request.OwnerId);

if (user != null) throw new RpcException(new Status(StatusCode.NotFound, "El usuario no existe."));

var newPost = new Post
{
OwnerId = request.OwnerId,
Title = request.Title,
Description = request.Description,
Image = request.Image,
};

_dbContext.Post.Add(newPost);
await _dbContext.SaveChangesAsync();

return new CreateReply
{
OwnerId = request.OwnerId,
Image = request.Image,
Title = request.Title,
Description = request.Description
};
}
}
65 replies
CC#
Created by Wiløvy on 12/9/2024 in #help
EF 9
// microservices/PostService/Models/Post.cs
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using UserModel.Models;

namespace PostModel.Models
{
public class Post
{
public int Id { get; set; }

[Required]
[ForeignKey("User")]
required public int OwnerId { get; set; }

public User User { get; set; } = null!;

[Required]
[MinLength(1), MaxLength(254)]
required public string Title { get; set; }

public string Description { get; set; } = null!;
public string Image { get; set; } = null!;
}
}
// microservices/PostService/Models/Post.cs
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using UserModel.Models;

namespace PostModel.Models
{
public class Post
{
public int Id { get; set; }

[Required]
[ForeignKey("User")]
required public int OwnerId { get; set; }

public User User { get; set; } = null!;

[Required]
[MinLength(1), MaxLength(254)]
required public string Title { get; set; }

public string Description { get; set; } = null!;
public string Image { get; set; } = null!;
}
}
65 replies