C
C#2y ago
Melnar

✅ Help me please

I am trying to do a project using asp.net identity. I am following n-layer mvc architecture, keeping the dal, bll and presentation layers separate connecting them with Startup classes. However the migration is not working. The project doesn't run either. Would anyone be willing to help? I can share my github.
14 Replies
Pobiega
Pobiega2y ago
$details
MODiX
MODiX2y ago
When you ask a question, make sure you include as much detail as possible. Such as code, the issue you are facing, and what you expect the result to be. Upload code here https://paste.mod.gg/ (see $code for more information on how to paste your code)
Pobiega
Pobiega2y ago
"the migration is not working" doesn't tell me anything. Is this EF Core? Is it creating or running the migration that doesn't work? What error messages?
Melnar
MelnarOP2y ago
EF Core
PM> add-migration TestDb
Build started...
Build succeeded.
An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: ExpenseTracker.BLL.Services.API.ITransactionsService Lifetime: Scoped ImplementationType: ExpenseTracker.BLL.Services.TransactionsService': Unable to resolve service for type 'ExpenseTracker.DAL.Entities.User' while attempting to activate 'ExpenseTracker.BLL.Services.TransactionsService'.)
No DbContext was found in assembly 'ExpenseTracker'. Ensure that you're using the correct assembly and that the type is neither abstract nor generic.
PM> add-migration TestDb
Build started...
Build succeeded.
An error occurred while accessing the Microsoft.Extensions.Hosting services. Continuing without the application service provider. Error: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: ExpenseTracker.BLL.Services.API.ITransactionsService Lifetime: Scoped ImplementationType: ExpenseTracker.BLL.Services.TransactionsService': Unable to resolve service for type 'ExpenseTracker.DAL.Entities.User' while attempting to activate 'ExpenseTracker.BLL.Services.TransactionsService'.)
No DbContext was found in assembly 'ExpenseTracker'. Ensure that you're using the correct assembly and that the type is neither abstract nor generic.
Pobiega
Pobiega2y ago
You seem to be trying to inject a user into transactionservice That seems like a very weird setup Show me the constructor for TransactionService
Melnar
MelnarOP2y ago
private readonly IUnitOfWork _unitOfWork;
private readonly IMapper _mapper;
private readonly User _loggedInUser;

public TransactionsService(IUnitOfWork unitOfWork, IMapper mapper, User loggedInUser)
{
_unitOfWork = unitOfWork;
_mapper = mapper;
_loggedInUser = loggedInUser;
}
private readonly IUnitOfWork _unitOfWork;
private readonly IMapper _mapper;
private readonly User _loggedInUser;

public TransactionsService(IUnitOfWork unitOfWork, IMapper mapper, User loggedInUser)
{
_unitOfWork = unitOfWork;
_mapper = mapper;
_loggedInUser = loggedInUser;
}
Pobiega
Pobiega2y ago
Yeah you are injecting a user Can't do that. Inject a http context accessor instead Or something that you can fetch the user from I usually make my own UserContext object for this purpose
Melnar
MelnarOP2y ago
How do i do that? I already have a user entity
public class User : IdentityUser<int>
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public DateTime CreatedDate { get; set; }
public decimal TotalIncome { get; set; }
public int RoleId { get; set; }
public Role Role { get; set; }
public ICollection<Category> Categories { get; set; }
public ICollection<Transaction> Transactions { get; set; }
}
public class User : IdentityUser<int>
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public DateTime CreatedDate { get; set; }
public decimal TotalIncome { get; set; }
public int RoleId { get; set; }
public Role Role { get; set; }
public ICollection<Category> Categories { get; set; }
public ICollection<Transaction> Transactions { get; set; }
}
Pobiega
Pobiega2y ago
Google http context Accessor You don't inject the user, you get it from the context
Melnar
MelnarOP2y ago
So the transaction entity will not need a user attribute to see who created it?
public class Transaction
{
public int Id { get; set; }
public decimal Amount { get; set; }
public DateTime Date { get; set; }
public bool IsExpense { get; set; }
public int CategoryId { get; set; }
public Category Category { get; set; }
public int UserId { get; set; }
public User User { get; set; }
}
public class Transaction
{
public int Id { get; set; }
public decimal Amount { get; set; }
public DateTime Date { get; set; }
public bool IsExpense { get; set; }
public int CategoryId { get; set; }
public Category Category { get; set; }
public int UserId { get; set; }
public User User { get; set; }
}
Pobiega
Pobiega2y ago
I'm not saying that I'm saying you can't inject a user
Melnar
MelnarOP2y ago
oh i see My bad I get it now Thanks
Pobiega
Pobiega2y ago
$close
MODiX
MODiX2y ago
Use the /close command to mark a forum thread as answered
Want results from more Discord servers?
Add your server