Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set a string column to nullable in EF6

I have a model that i created it bu EF6:

public partial class Comment
{
    [DisplayName("شناسه نظر")]
    public int Id { get; set; }

    [Required(ErrorMessage = "متن نظر را وارد کنید")]
    [DisplayName("متن نظر")]
    public string CommentText { get; set; }

    [DisplayName("تعداد پسندیدن ")]
    public long LikeCount { get; set; }

    [DisplayName("تعداد نپسندیدن")]
    public long DisLikeCount { get; set; }

    [DisplayName("تاریخ انتشار ")]
    public System.DateTime PublishDate { get; set; }

    [DisplayName("وضعیت نمایش ")]
    public string Visible { get; set; }

    [DisplayName("نام کاربری ")]
    public Nullable<string> AutherUserName { get; set; }

    [DisplayName("شناسه نظراصلی")]
    public Nullable<int> CommentFKId { get; set; }

    [DisplayName("شناسه کاربر")]
    public Nullable<int> StudentId { get; set; }

    [DisplayName("شناسه محتوا ")]
    public Nullable<int> ContentId { get; set; }

    public virtual Comment Comments { get; set; }
    public virtual Comment Comment1 { get; set; }
    public virtual Student Student { get; set; }
    public virtual Content Content { get; set; }
}

As you can see i have several Nullable int columns in my model ,But i can't set a string column to null :

public Nullable<string> AutherUserName { get; set; }

And I got this error :

The type 'string' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable'

I am working with MVC4

like image 935
Ehsan Akbar Avatar asked Apr 05 '14 20:04

Ehsan Akbar


1 Answers

Strings are reference types, so are already "nullable". Only value types (such as int) can be nullable as they can't otherwise be null.

like image 155
Martin Costello Avatar answered Oct 01 '22 03:10

Martin Costello