简体   繁体   English

在EF6中将字符串列设置为可为空

[英]Set a string column to nullable in EF6

I have a model that i created it bu EF6: 我有一个通过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 : 如您所见,我的模型中有几个Nullable int列,但是我无法将字符串列设置为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' 为了在通用类型或方法“ System.Nullable”中将其用作参数“ T”,类型“字符串”必须为非空值类型

I am working with MVC4 我正在使用MVC4

Strings are reference types, so are already "nullable". 字符串是引用类型,因此已经是“可为空”。 Only value types (such as int) can be nullable as they can't otherwise be null. 只有值类型(例如int)可以为空,否则它们不能为null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM