Ehsan Akbar
Ehsan Akbar

Reputation: 7301

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

Upvotes: 17

Views: 34885

Answers (1)

Martin Costello
Martin Costello

Reputation: 10863

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

Upvotes: 36

Related Questions