Determining If Entity Is Unique In Repository Pattern

Brian Ashcraft 100 Reputation points
2023-03-01T05:27:49.4533333+00:00

Hello,

Using c# and ASP .Net Core 6 I have an N-Tier web application that has implemented a Repository Pattern and Unit of Work.

In this web application, an Admin can create a Hashtag, based on the following model:

 public class Hashtag
 {
     [Key]
     public int Id { get; set; }

     [Required]
     [StringLength(50, ErrorMessage = "Name cannot be longer than 50 characters.")]
     public string Name { get; set; }

     [Required]
     [StringLength(100, ErrorMessage = "Description cannot be longer than 100 characters.")]
     public string Description { get; set; }
 }

In the IHashtagRepository.cs I have added the following Method:

public interface IHashtagRepository : IRepository<Hashtag>
{
    bool CheckIfHashtagExists(string name);
}

In the HashtagRepository.cs I have the following:

public bool CheckIfHashtagExists(string name)
{
    var objFromDb = _db.Find(e => e.Name == name);
    return objFromDb.Any();
}

In the OnPost() Method of the Create.cshtml.cs I would like to ensure that the Hashtag.Name is distinct (not a duplicate).

I though of doing something like this:

var objExists = CheckIfHashtagExists(somenamehere);
if (objExists = True)
{
     Do some stuff
}
else
{
    Do something else
}

I am getting an error on this line in HashtagRepository.cs

var objFromDb = _db.Find(e => e.Name == name);

Compiler Error CS1061 type' does not contain a definition for 'name' and no accessible extension method 'name' accepting a first argument of type 'type' could be found (are you missing a using directive or an assembly reference?).

I am unsure how to correct this error.

Also, I am unsure if there is a more correct way to achieve what I am attempting to do.

My Generic Repository has the following Mathods available in the Interface:

public interface IRepository<T> where T : class
{
    T Get(int id);
    IEnumerable<T> GetAll();
    IEnumerable<T> Find(Expression<Func<T, bool>> expression);
    T GetFirstOrDefault(Expression<Func<T, bool>>? filter = null, string? includeProperties = null);
    void Add(T entity);
    void Remove(int id);
    void Remove(T entity);
    void RemoveRange(IEnumerable<T> entity);
}

Thanks in advance for your help.

It is much appreciated.

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
697 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,167 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,256 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,253 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 24,286 Reputation points Microsoft Vendor
    2023-03-01T08:37:01.1466667+00:00

    @Brian Ashcraft, Welcome to Microsoft Q&A, based on my test, I reproduced your problem.

    As the Microsoft Learning DbContext.Find Method described, the method is used to find an entity with the given primary key values.

    Therefore, you could not use Find method for non-primary key field.

    You could try the following code to check if Hashtag.Name is distinct.

    public bool CheckIfHashtagExists(string name)
    {
        var objFromDb3 = context.hashtags.Where(m => m.Name == name).Select(m => m.Name);
        var result = objFromDb3.Count() > 0 ? true : false; //->First Method
        var result1= objFromDb3.Any() //->Second Method
    
        return result;
    }
    

    Hope my code could help you.

    Best Regards,

    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful