C
C#3w ago
Zee

✅ code cant find reference to my ID but I am implementing it correctly

Severity Code Description Project File Line Suppression State Details Error CS0103 The name 'customers' does not exist in the current context PharmacyApp D:\Zeeshan\Documents\Pharmacy\PharmacyApp\Services\FeedbackFormService.cs 27 Active
using Microsoft.AspNetCore.Identity;
using PharmacyApp.Models;
using PharmacyApp.Repositories;
using PharmacyApp.ViewModels;

namespace PharmacyApp.Services
{
public class FeedbackFormService : IFeedbackFormService<FeedBackFormViewModel>
{
private readonly IRepository<FeedbackForm> _repository;
private readonly UserManager<IdentityUser> _userManager;


public FeedbackFormService(IRepository<FeedbackForm> repository, UserManager<IdentityUser> userManager)
{
_repository = repository;
_userManager = userManager;
}

public async Task<FeedBackFormViewModel> CreateForm(FeedBackFormViewModel entity)
{

var model = new FeedbackForm
{
Title = entity.Title,
Description = entity.Description,
CustomerID = _userManager.GetUserId(customers)
};

// Add to repository
await _repository.AddAsync(model);

return entity;

}
}
}
using Microsoft.AspNetCore.Identity;
using PharmacyApp.Models;
using PharmacyApp.Repositories;
using PharmacyApp.ViewModels;

namespace PharmacyApp.Services
{
public class FeedbackFormService : IFeedbackFormService<FeedBackFormViewModel>
{
private readonly IRepository<FeedbackForm> _repository;
private readonly UserManager<IdentityUser> _userManager;


public FeedbackFormService(IRepository<FeedbackForm> repository, UserManager<IdentityUser> userManager)
{
_repository = repository;
_userManager = userManager;
}

public async Task<FeedBackFormViewModel> CreateForm(FeedBackFormViewModel entity)
{

var model = new FeedbackForm
{
Title = entity.Title,
Description = entity.Description,
CustomerID = _userManager.GetUserId(customers)
};

// Add to repository
await _repository.AddAsync(model);

return entity;

}
}
}
there error is on this line
userID = _userManager.GetUserId(customers)
userID = _userManager.GetUserId(customers)
I checked my DI in program.cs its all right to me
77 Replies
Zee
ZeeOP3w ago
this is my models class
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Zee
ZeeOP3w ago
thats not throwing me any error though and the udemy tutorial I am follwoing he did it and its fine
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Zee
ZeeOP3w ago
yes
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Zee
ZeeOP3w ago
so then why do ppl follow the repository flow if its just doing basic stuff liek that is it more reserved for more complex queiries
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Zee
ZeeOP3w ago
ok lol he said its what the pros uses
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Zee
ZeeOP3w ago
I just did bexcause its in the vid yh I agree it did seem a bit redundant
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Zee
ZeeOP3w ago
sure
public interface IRepository<T> where T : class
{
Task<IEnumerable<T>> GetAllAsync();
Task<T> GetByIdAsync(int id);
Task<T> GetByEmailAsync(string email);

Task<T> GetByNameAsync(string name);


Task AddAsync(T entity);
Task UpdateAsync(T entity);

Task DeleteAsync(int id);
Task<T> FindAsync(Expression<Func<T, bool>> predicate);
Task<IEnumerable<T>> FindAllAsync(Func<T, bool> predicate);


}
public interface IRepository<T> where T : class
{
Task<IEnumerable<T>> GetAllAsync();
Task<T> GetByIdAsync(int id);
Task<T> GetByEmailAsync(string email);

Task<T> GetByNameAsync(string name);


Task AddAsync(T entity);
Task UpdateAsync(T entity);

Task DeleteAsync(int id);
Task<T> FindAsync(Expression<Func<T, bool>> predicate);
Task<IEnumerable<T>> FindAllAsync(Func<T, bool> predicate);


}
it is a bit odd though since I was using it for so many classes so I have dumb ones like GetByEmail for user loggin I saw some ppl making ICustomerRepos>CustomerRepos IProductRepos>ProductRepos but that just seems pointless too
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Zee
ZeeOP3w ago
oh right soz
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Zee
ZeeOP3w ago
namespace PharmacyApp.Repositories
{
public class FeedbackFormRepository : IRepository<FeedbackForm>
{
private readonly ApplicationdbContext _context;

public FeedbackFormRepository(ApplicationdbContext context)
{
_context = context;
}
public async Task<FeedbackForm> AddAsync(FeedbackForm entity)
{
var add = await _context.FeedbackForm.AddAsync(entity);
_context.SaveChangesAsync();
return entity;
}

public async Task DeleteAsync(int id)
{
var findForm = await _context.FeedbackForm.FindAsync(id);
if (findForm != null)
{
throw new Exception();
}
_context.Remove(findForm);
await _context.SaveChangesAsync();
}

public Task<IEnumerable<FeedbackForm>> FindAllAsync(Func<FeedbackForm, bool> predicate)
{
throw new NotImplementedException();
}

public Task<FeedbackForm> FindAsync(Expression<Func<FeedbackForm, bool>> predicate)
{
throw new NotImplementedException();
}

public async Task<IEnumerable<FeedbackForm>> GetAllAsync()
{
return await _context.FeedbackForm.ToListAsync();

}


}
namespace PharmacyApp.Repositories
{
public class FeedbackFormRepository : IRepository<FeedbackForm>
{
private readonly ApplicationdbContext _context;

public FeedbackFormRepository(ApplicationdbContext context)
{
_context = context;
}
public async Task<FeedbackForm> AddAsync(FeedbackForm entity)
{
var add = await _context.FeedbackForm.AddAsync(entity);
_context.SaveChangesAsync();
return entity;
}

public async Task DeleteAsync(int id)
{
var findForm = await _context.FeedbackForm.FindAsync(id);
if (findForm != null)
{
throw new Exception();
}
_context.Remove(findForm);
await _context.SaveChangesAsync();
}

public Task<IEnumerable<FeedbackForm>> FindAllAsync(Func<FeedbackForm, bool> predicate)
{
throw new NotImplementedException();
}

public Task<FeedbackForm> FindAsync(Expression<Func<FeedbackForm, bool>> predicate)
{
throw new NotImplementedException();
}

public async Task<IEnumerable<FeedbackForm>> GetAllAsync()
{
return await _context.FeedbackForm.ToListAsync();

}


}
public Task<FeedbackForm> GetByEmailAsync(string email)
{
throw new NotImplementedException();
}

public async Task<FeedbackForm> GetByIdAsync(int id)
{
var find = await _context.FeedbackForm.FindAsync(id);
if (find != null)
{
throw new Exception();
}
return find;

}

public Task<FeedbackForm> GetByNameAsync(string name)
{
throw new NotImplementedException();
}

public async Task UpdateAsync(FeedbackForm entity)
{
var update = _context.FeedbackForm.Update(entity);
await _context.SaveChangesAsync();
}

Task IRepository<FeedbackForm>.AddAsync(FeedbackForm entity)
{
throw new NotImplementedException();
}
}
public Task<FeedbackForm> GetByEmailAsync(string email)
{
throw new NotImplementedException();
}

public async Task<FeedbackForm> GetByIdAsync(int id)
{
var find = await _context.FeedbackForm.FindAsync(id);
if (find != null)
{
throw new Exception();
}
return find;

}

public Task<FeedbackForm> GetByNameAsync(string name)
{
throw new NotImplementedException();
}

public async Task UpdateAsync(FeedbackForm entity)
{
var update = _context.FeedbackForm.Update(entity);
await _context.SaveChangesAsync();
}

Task IRepository<FeedbackForm>.AddAsync(FeedbackForm entity)
{
throw new NotImplementedException();
}
}
lol yeah
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Zee
ZeeOP3w ago
yh in the database the id is made on the fly
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Zee
ZeeOP3w ago
yh it seems like a duplicate
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Zee
ZeeOP3w ago
CustomerID = _userManager.GetUserId(customers) in the feedback service class
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Zee
ZeeOP3w ago
yh he did it on the tutorial its supposed ot be a ref to my foreign ID in the model class i did show earlier should I show it again error cos u didnt define a ?
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Zee
ZeeOP3w ago
public class FeedbackForm
{
[Key]
[Required]
public int Id { get; set; }

public string Title { get; set; }
[Required]
public string Description { get; set; }

[Required]
public string CustomerID { get; set; }

[ForeignKey(nameof(CustomerID))]
public IdentityUser customers { get; set; }
}
public class FeedbackForm
{
[Key]
[Required]
public int Id { get; set; }

public string Title { get; set; }
[Required]
public string Description { get; set; }

[Required]
public string CustomerID { get; set; }

[ForeignKey(nameof(CustomerID))]
public IdentityUser customers { get; set; }
}
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Zee
ZeeOP3w ago
erm it sort of makes sense to me it was a ref to the id
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Zee
ZeeOP3w ago
no I just started the tutorial
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Zee
ZeeOP3w ago
yes this is assigning the FK which is of type IdentityUser and is named CustomerID yes
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Zee
ZeeOP3w ago
yh refs another table
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Zee
ZeeOP3w ago
feedbackform refs IdentityUser which is where the CustomerID soz I dont understand I am using MMSQL to store the database
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Zee
ZeeOP3w ago
yes card
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Zee
ZeeOP3w ago
ID? int I mean
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Zee
ZeeOP3w ago
yh
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Zee
ZeeOP3w ago
ill give it a quick read and come back
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Zee
ZeeOP3w ago
CustomerID is a foreign key that goes toI IdentityUser
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Zee
ZeeOP3w ago
well wouldnt that random proerty be part of the entiy? CreateForm(FeedBackFormViewModel entity) I get that ur not refs the class so it doesnt know
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Zee
ZeeOP3w ago
He did this
No description
Zee
ZeeOP3w ago
And it worked
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Zee
ZeeOP3w ago
he did it for a job posting I am doing a customer feedbacForm
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Zee
ZeeOP3w ago
He in the controller file yes but I was reading online and I saw that’s it better to have this type of code in a service layer so it separates the business logic more
Zee
ZeeOP3w ago
It’s the foreign key
No description
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Zee
ZeeOP3w ago
Nah I dont
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Zee
ZeeOP3w ago
I don’t understand this your saying he only refs it once so that’s why it’s not causing him problems
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Zee
ZeeOP3w ago
nope this is my intro to it
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Zee
ZeeOP3w ago
yh it does work
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Zee
ZeeOP3w ago
cos hes in the controller
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Zee
ZeeOP3w ago
so thecontroller.User means the JobPosting.User? his controller name is JobPosting
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Zee
ZeeOP3w ago
I think its 3 cos I followed everthing before and it worked
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Zee
ZeeOP3w ago
right I get u so I have to make one or move the code to the controller
Unknown User
Unknown User3w ago
Message Not Public
Sign In & Join Server To View
Zee
ZeeOP3w ago
hmm thx I think I will start this again

Did you find this page helpful?