Enums in C# can be saved as strings in a database by using an ORM (Object-Relational Mapping) library or by manually converting the enum values to strings before storing them in the database.
If you are using an ORM library, such as Entity Framework or NHibernate, you can map the enum property to a string column in the database by using the EnumToStringConverter
class provided by the ORM library. Here's an example using Entity Framework:
public enum Gender { Male, Female, Other } public class Person { public int Id { get; set; } public string Name { get; set; } public Gender Gender { get; set; } } public class MyContext : DbContext { public DbSet<Person> People { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Person>() .Property(p => p.Gender) .HasConversion<string>(); } }
In the above example, the Gender
enum property of the Person
class is mapped to a string column in the database using the HasConversion<string>()
method. This method tells Entity Framework to use the EnumToStringConverter
class to convert the enum value to a string before storing it in the database.
If you are not using an ORM library, you can manually convert the enum values to strings before storing them in the database. Here's an example using ADO.NET:
public enum Gender { Male, Female, Other } public class Person { public int Id { get; set; } public string Name { get; set; } public Gender Gender { get; set; } } public class MyDatabase { private readonly string connectionString; public MyDatabase(string connectionString) { this.connectionString = connectionString; } public void SavePerson(Person person) { using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand command = new SqlCommand("INSERT INTO People (Name, Gender) VALUES (@Name, @Gender)", connection); command.Parameters.AddWithValue("@Name", person.Name); command.Parameters.AddWithValue("@Gender", person.Gender.ToString()); connection.Open(); command.ExecuteNonQuery(); } } }
In the above example, the Gender
enum value of the Person
class is converted to a string using the ToString()
method before being stored in the Gender
column of the People
table in the database.
Note that when reading the enum values back from the database, you will need to convert the string values back to their corresponding enum values using the Enum.Parse()
method or a similar method.
"C# store enum as string in database"
// Code to store enum as string in database public enum Status { Active, Inactive, Pending } [Column(TypeName = "nvarchar(20)")] public string StatusAsString { get; set; }
"C# entity framework enum to string mapping"
// Code to map enum to string column in Entity Framework public class YourEntity { public Status Status { get; set; } [Column(TypeName = "nvarchar(20)")] public string StatusAsString { get => Status.ToString(); set => Status = (Status)Enum.Parse(typeof(Status), value); } }
"C# enum serialization to database as string"
// Code to serialize enum to string for database storage public enum Status { Active, Inactive, Pending } string statusAsString = JsonConvert.SerializeObject(Status.Active); Status status = JsonConvert.DeserializeObject<Status>(statusAsString);
"C# enum string conversion for database"
// Code to convert enum to string and vice versa for database public enum Status { Active, Inactive, Pending } string statusAsString = Enum.GetName(typeof(Status), Status.Active); Status status = (Status)Enum.Parse(typeof(Status), statusAsString);
"C# code-first enum as string in database"
// Code-first approach to store enum as string in database public class YourEntity { public Status Status { get; set; } [Column(TypeName = "nvarchar(20)")] public string StatusAsString { get; set; } }
"C# enum attribute for database storage"
// Code to use attribute for database storage of enum public enum Status { [Column("ActiveStatus")] Active, [Column("InactiveStatus")] Inactive, [Column("PendingStatus")] Pending }
"C# enum string mapping with Dapper"
// Code to map enum to string column using Dapper var status = connection.QueryFirstOrDefault<Status>("SELECT StatusAsString FROM YourTable WHERE Id = @Id", new { Id = 1 });
"C# enum to string mapping with NHibernate"
// Code to map enum to string column using NHibernate Map(x => x.StatusAsString).CustomType<YourEnumTypeAsString>();
hsv ansi-sql xamarin-studio y2k adobe-animate validationrules datatables datatable pgrouting google-font-api