Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enums casting to a byte

Am using EntityFramework and have a LinkStatusID column which is a tinyint, which gets generated into a byte in C#.

public enum LinkStatus
{
    Added = 0,
    Deleted = 1
}

however this gives:

a.LinkStatusID = (byte)Enums.LinkStatus.Deleted;

is there a more elegant way to structure this?

EDIT2 for LastCoder:

public enum LinkStatus : byte
{
    Added = 0,
    Deleted = 1
}

    var blah = Enums.LinkStatus.Added;
    var ty = blah.GetType();

    var blah2 = (byte)Enums.LinkStatus.Added;
    var ty2 = blah2.GetType();

This doesn't work (as I expected) however the first answer here explains why.

EDIT3: EF isn't the only way this sln gets to the DB, so I'm keen to keep the Enums explicit in the code. Thanks for EF5 Enum suggestions!

like image 380
Dave Mateer Avatar asked Apr 20 '26 17:04

Dave Mateer


1 Answers

public enum LinkStatus : byte

Will avoid the explicit cast.

like image 133
Louis Ricci Avatar answered Apr 23 '26 07:04

Louis Ricci