Search code examples
c#sqlasp.net-mvcdatabaselocaldb

Enumlike object's(Person) status field class in ASP .NET MVC


I would like to create class Person which will have int Id; string Name etc. but there is one thing I don't know how to solve.

I'll be calling every person and after the call I would like to set status for the result of call and I want only those: "SUCCESS", "DIDN'T WANT TO TALK", "CALL LATER", DID NOT PICK UP THE PHONE and that's all no more. Of course all objects of class Person will be in the LocalDb database.

As far as I remember it is called dictionary entity in databases.

How should I create this in C# one class, two classes? Is there any method to solve this to add only possible values?

I do not know if Entity Framework with deal with enum. Could somebody writa a minimal sample of Person and this Enum type which Entity Framework will understand?


Solution

  • Entity framework can handle enums, but from a readability viewpoint, it is not a great practice. It would mean anyone looking at the database or reporting from the database in the future would only see a meaningless integer.

    A better way to do it would be use another entity and seed the database with the values you want.

    public class Call
    {
        ..... //some other properties you need
        public int StatusId {get;set;}    
        public virtual Status Status {get;set}
    }
    
    public class Status
    {
        public int Id {get;set;}
        public int Name {get;set;}
    }
    

    You would then just seed the 3 options into status.

    When adding or updating a call, you only need to assign the seeded ID of the status to StatusId as that is your FK. The virtual status property is your navigation property. So when you are accessing a status on a call you can just use Call.Status.Name or Call.Status.Id to access which status property you want to use.