login to rate this
.NET BlogEngine 4.0 [non-official]
2 years ago
0
0
|
login to rate this
[.NET7] Design Patterns: Facade in a Web Application
2 years ago
8
0
|
login to rate this
[.NET7] Design Patterns: Repository in a Web Application
3 years ago
0
0
|
login to rate this
[.NET7] IP GeoLocation with Dependency Injection and Custom Middleware
3 years ago
0
0
|
login to rate this
[.NET7] Weather Forecast with Dependency Injection and Custom Middleware
2 years ago
0
0
|
login to rate this
2 ways for creating QR Code using ASP.NET
8 years ago
0
0
|
login to rate this
AMD Ryzen 9 home server with Fedora 41 and .NET 8
one year ago
30
1
|
login to rate this
ASP.NET MVC Routing with @
9 years ago
0
0
|
login to rate this
ASP.NET MVC Routing with Constraints
9 years ago
0
0
|
login to rate this
BlogEngine.NET 3.3.5 with a BBQ Sauce
4 years ago
0
0
|
login to rate this
BlogEngine.NET 3.3.5 with a BBQ Sauce: Part 2
4 years ago
0
0
|
login to rate this
EF7 on .NET Core 7 (Preview): Fedora 35 x Windows 11 (41 Million Rows)
4 years ago
1
0
|
login to rate this
Entity Framework Core 6: Fedora 35 x Windows 11 (41 Million Rows)
4 years ago
2
0
|
login to rate this
Entity Framework Core 7 (Preview): Fedora 35 x Windows 11 (41 Million Rows)
4 years ago
0
0
|
login to rate this
Entity Framework Providers for the .NET BlogEngine
4 years ago
0
0
|
login to rate this
Enum as String in EF Core
2 years ago
1
0
|
login to rate this
Facebook: Data Deletion Callback URL in C#
4 years ago
0
0
|
login to rate this
How to fix the Could not load file or assembly 'System.IO.Compression' or one of its dependencies exception
4 years ago
0
0
|
login to rate this
How to Recover Access to your Account in an ASP.NET Application using the Membership (without decrypting your password)
4 years ago
0
0
|
login to rate this
Installing and Configuring Jenkins with GIT to Build and Deploy Your .NET Application
2 years ago
0
0
|
login to rate this
Installing and Configuring Jenkins with SVN to Build and Deploy Your .NET Application
2 years ago
0
0
|
login to rate this
IP GeoLocation with Unity and Dependency Injection
4 years ago
0
0
|
login to rate this
NHibernate Providers for the .NET BlogEngine
4 years ago
0
0
|
login to rate this
No mercy for the ORMs: Load and Performance Test on EF Core, NHibernate and pure ADO.NET (41M rows)
4 years ago
0
0
|
login to rate this
Resizing images on a Controller Method in ASP.NET Core (.NET 6)
4 years ago
0
0
|
login to rate this
Streaming Video Files in ASP.NET MVC
4 years ago
0
0
|
login to rate this
Testing your MVC App on XSP/mono in Fedora (before deploying to Apache)
4 years ago
0
0
|
login to rate this
Weather with Unity and Dependency Injection
4 years ago
0
0
|
nbolt
Hi Folks! In this post I will show you how to save an enum property from your POCO class as a string in the database, using EF core. A throughout explanation can be found in the Microsoft website here. So, perhaps you just need to translate those numbers into something more readable in your SQL table. The short answer can be found below:
public class SocialProfile {
[StringLength(15)]
public SocialNetworks SocialNetwork { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public enum SocialNetworks: byte {
Facebook = 1,
Google = 2,
Twitter = 3,
GitHub = 4,
}
I prefer using the StringLength attribute instead of the main approach in the Microsoft website (column attribute with type name nvarchar), because it's database agnostic, this code works with several types of databases. If your application supports them, it's a good idea to use StringLength and save you countless conditions to handle the types of databases into your OnModelCreating database context method.
You only need to be careful with the column size as the StringLength parameter, be sure to make room for future values in your enum-string situation. For example, if you need to deal with a new social network called bla-bla-bla-bla-bla (with a big name, you would have to update the table column size, if the current one isn't enough).
2. Second Approach: OnModelCreating, HasConversion method
Well, following the same logic, we can handle the enum-string conversion directly in your OnModelCreating method, using an instance of the ValueConverter class:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var converter = new ValueConverter<SocialNetworks, string>(
v => v.ToString(),
v => (SocialNetworks)Enum.Parse(typeof(SocialNetworks), v));
modelBuilder
.Entity<SocialProfile>()
.Property(e => e.SocialNetwork)
.HasConversion(converter);
}
If your situation is more complicated than that, you can inherit ValueConverter into your own class and handle however you need to.
3. Third Approach: OnModelCreating, HasConversion<string> method
Another interesting approach: we can use built-in converters. So instead of declaring the ValueConverter class, like the above, we can just use the string type as a generic parameter and the conversion is done. Check it out:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder
.Entity<SocialProfile>()
.Property(e => e.SocialNetwork)
.HasConversion<string>();
}
Go to the reference website (the Microsoft one at the top) to check the list of built-in converters ready to be used and coded by them.
And that would be it! Feel free to ask any questions you might have in the comment section below, I will do my best to answer them.
Hope you guys enjoyed it. See ya!
nbolt
nbolt
nbolt

We use cookies and similar technologies to enhance your browsing experience, analyze site traffic, personalize content, and serve targeted advertisements. By continuing to use our site, you consent to our use of cookies. And click OK to close this pop-up window.