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

标签 c# asp.net-mvc entity-framework asp.net-mvc-4

我有一个使用 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; }
}

如您所见,我的模型中有多个Nullable int 列,但我无法将字符串列设置为空:

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

我得到了这个错误:

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'

我正在使用 MVC4

最佳答案

字符串是引用类型,因此已经是“可为空的”。只有值类型(如 int)可以为 null,否则它们不能为 null。

关于c# - 在 EF6 中将字符串列设置为可为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22886405/

相关文章:

asp.net - OData 和 Entity Framework 不同模型映射

c# - 在 asp.net 中创建连接字符串/连接到数据库?

c# - 检查空 HttpContext.Current.Cache 值

asp.net - ASP.NET MVC 路由和经典的 ASP 主页(VBScript,不是 Web 表单)可以很好地协同工作吗?

c# - Entityframework RC1 - MetadataItem.Annotations made internal - 替代方案?

c# - 如何在一个模型中选择不同表格的内容

C# dt.Compute() 在某些乘法上不能正常工作

c# - 派生类中复杂对象的类型转换

jquery - 将字符串数组从 MVC 返回到 jQuery ASP.NET

asp.net - 创建 "Live"ASP.NET MVC 应用程序