0

I have to tables created theme by Entity Framework for example: Book Table:

public class Book
{
   public int Id { get; set; }
   public string Title { get; set; }
   public int Book_Type_Id { get; set; }
}

Book_Type Table:

public Book_Type
{
   public int Id { get; set; }
   public string Title { get; set; }
}

I will store some limited values for example Novel and education and ... in Book_Type table But in this way searching string can has spelling error. I think it's cannot be correct and may be exist a way for example use a predefined list and store specific Book type value as string. what is your opinion?

Babak irannezhad
  • 302
  • 4
  • 19

1 Answers1

1

You maybe looking for an Enum. A value type data type, where you decalre all distinct possible value. In order to have those name constant and easly refered to.

Ef does support the enum. And it's pretty strait forward. Declare then Use it.
Here is a msdn article and video about MSDN: Enum Support - Code First

xdtTransform
  • 1,986
  • 14
  • 34
  • But In using enum, If we want to find 'novel' books we must search the books that their book_type equal to 'novel' , there isn't other way to liberate querying string? – Babak irannezhad Dec 03 '19 at 14:21
  • 1
    You will find a way to cast string back to enum [here](https://stackoverflow.com/questions/16100/convert-a-string-to-an-enum-in-c-sharp). But it look weird to have a string there when you can have a people directly selecting a Enum value with a dropdown, checkbox or anything. – xdtTransform Dec 03 '19 at 14:39