2017-08-03 30 views
-1

我已經創建了我的模型,但whem我想添加一個新的attribut,總是出現這樣的錯誤:如何在ASP中添加一個新的attribut在你的模型類的MVC

Severity Code Description Project File Line Suppression State 
Error CS1519 Invalid token '}' in class, struct, or interface member declaration CarManagement C:\Users\aganfoud\Documents\Visual Studio 2015\Projects\CarManagement\CarManagement\Models\Voiture.cs 33 Active 

這是我的模型:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.ComponentModel.DataAnnotations; 

namespace CarManagement.Models 
{ 
    public class Voiture 
    { 
     public int ID { get; set; } 
     [StringLength(60, MinimumLength = 3)] 
     [Required] 

     public string Marque { get; set; } 
     [Required] 
     [StringLength(30)] 

     public string Modele { get; set; } 

     [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true) Display(Name = "Construction Date") 
     DataType(DataType.Date)] 
     public DateTime ConstructionDate { get; set; } 

     [Required] 
     [Range(1, 99999)] 
     [DataType(DataType.Currency)] 
     public decimal Price { get; set; } 

     public string Color { get; set; } 
     [Required] 
     [StringLength(30)] 
    } 
} 

,當我想要添加的attribut 顏色,如果沒有這個attribut出現錯誤,他的作品完美

+0

嘗試把上述特性的屬性 –

+0

你也是一個「」在失蹤在ContructinDate上結合屬性。可能是複製/發佈錯誤。 –

+0

謝謝,它的工作原理沒有這個「」我不知道問題所在,怎麼我現在有一個例外puting上述屬性後: 類型的異常‘System.Data.Entity.Core.EntityCommandExecutionException’發生的EntityFramework。 SqlServer.dll但沒有在用戶代碼 – Anouar

回答

1

屬性總是以上方法或財產聲明。它們適用於接下來的內容。

您的最後一組屬性後面沒有跟着方法或屬性聲明,因此編譯器無法知道它應該應用於什麼,因此出現錯誤。

因此,使它像這樣:

public class Voiture 
{ 
    [Required] 
    [StringLength(60, MinimumLength = 3)] 
    public int ID { get; set; } 

    [Required] 
    [StringLength(30)] 
    public string Marque { get; set; } 

    ... 

    [Required] 
    [StringLength(30)] 
    public string Color { get; set; } 
} 
+0

處理好啊,謝謝你回答我,我明白,但我該怎麼辦才能解決它? – Anouar

+0

將屬性行放在Color屬性上方,並對所有其他屬性執行相同的操作,以確保它們正確應用! –

+0

我做到了,但我知道有一個例外: 型「System.Data.Entity.Core.EntityCommandExecutionException」的異常出現在EntityFramework.SqlServer.dll但在用戶代碼中沒有處理 – Anouar

0

屬性去道具上面不低於

[Required] 
[StringLength(30)] 
public string Color { get; set; } 
+0

感謝您的答案,我做到了,但我知道一個例外: 在EntityFramework.SqlServer.dll中發生類型'System.Data.Entity.Core.EntityCommandExecutionException'的異常,但未處理用戶代碼 – Anouar

+0

我想你先使用代碼,然後你需要更新數據庫以及 –

+0

啓用遷移和更新數據庫鏈接https://msdn.microsoft.com/en-us/library/jj591621(v=vs.113)的.aspx –

相關問題