2014-04-05 23 views
10

null bir dize sütunu ayarlayın: benim modelinde birkaç null int sütunlarına sahip görebilirsiniz Ama yapamam gibiBen Ef6 met oluşturulduğu bir modele sahip 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; } 
} 

null bir dize sütunu ayarlayın:

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

Ve bu hata var:

tip 'dizesi' olmayan bir olmalı genel türü veya yöntem 'System.Nullable' ı MVC4 ile çalışıyorum

+0

Olası http://stackoverflow.com/questions/9236468/the-type-string-must-be-a-non-nullable-type-in-order-to-use-it-as-parameter- çoğaltması t –

+0

UI'niz bir boşluk verdiyse bile dizede null olmasını istiyorsanız, bunu özellik kümesinde yapabilirsiniz. –

cevap

19

Yaylı böylece, referans türleri zaten "olabilecek olan parametre olarak 'T' kullanmak amacıyla -nullable değer türü ". Aksi halde boş bırakılamadığı için yalnızca değer türleri (int gibi) geçersiz olabilir.

+0

Yani, sütümün metin kutusu boşsa, boş olacak mı? –

+4

Modeldeki dize özelliğinin değeri boşsa, o zaman boş olacak. "Boş" ise (boş dizge), o zaman boş olmaz. Bir dize sütununun null olmayan olması için, [Zorunlu] özelliğini de kullanırsınız. –

+0

Eğer Database-first 'not-null' tasarımım düz' string' özelliği ile bir sınıf oluşturduysa herhangi bir öneride bulunabilirsiniz. [Zorunlu] özniteliğini içermiyordu, ancak ModelBuilding'inde, muhtemelen sütunun "boş değil" olduğunu saptadı; Mülkün null olmasını sağlamak için akıcı bir yol var mı? Ve değilse değiştirilsin mi? –

İlgili konular