簡體   English   中英

腳手架新視圖時的驗證錯誤

[英]Validation errors when scaffolding a new view

編輯2:

我通過從DbSet中刪除我的POCO類之一來解決此問題。 這是消除過程的開始,以查看其中是否有任何問題的原因。

罪犯是這樣的:

public class Error
{
    public int Id { get; set; }
    public string Component { get; set; }
    public string Action { get; set; }
    public DateTime Date { get; set; }
    public Exception Exception { get; set; }
}

有什么想法為什么會讓人厭煩嗎?

編輯1:

當我嘗試添加數據遷移時也會發生錯誤。 從軟件包管理器控制台:

One or more validation errors were detected during model generation:

SuppliersMVC.Models.Exception: : EntityType 'Exception' has no key defined. Define the key for this EntityType.
SuppliersMVC.Models.IdentityReference: : EntityType 'IdentityReference' has no key defined. Define the key for this EntityType.
SuppliersMVC.Models.AssemblyName: : EntityType 'AssemblyName' has no key defined. Define the key for this EntityType.
SuppliersMVC.Models.CultureInfo: : EntityType 'CultureInfo' has no key defined. Define the key for this EntityType.
SuppliersMVC.Models.DateTimeFormatInfo: : EntityType 'DateTimeFormatInfo' has no key defined. Define the key for this EntityType.
Exceptions: EntityType: EntitySet 'Exceptions' is based on type 'Exception' that has no keys defined.
IdentityReferences: EntityType: EntitySet 'IdentityReferences' is based on type 'IdentityReference' that has no keys defined.
AssemblyNames: EntityType: EntitySet 'AssemblyNames' is based on type 'AssemblyName' that has no keys defined.
CultureInfoes: EntityType: EntitySet 'CultureInfoes' is based on type 'CultureInfo' that has no keys defined.
DateTimeFormatInfoes: EntityType: EntitySet 'DateTimeFormatInfoes' is based on type 'DateTimeFormatInfo' that has no keys defined.

這是我的DbContext:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public virtual DbSet<Error> Errors { get; set; }
    public virtual DbSet<PaymentTerm> PaymentTerms { get; set; }
    public virtual DbSet<Supplier> Suppliers { get; set; }
    public virtual DbSet<SupplierDocument> SupplierDocuments { get; set; }
    public virtual DbSet<SupplierService> SupplierServices { get; set; }
    public virtual DbSet<Voucher> Vouchers { get; set; }

    public ApplicationDbContext()
        : base("DevConnection", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

錯誤中引用的類( SuppliersMVC.Models.ExceptionSuppliersMVC.Models.IdentityReference等)也沒有出現在我的對象瀏覽器中。

還值得注意的是,該項目的構建沒有任何錯誤。 當我嘗試添加遷移時,“輸出”窗口不顯示任何內容。

我正在嘗試擴展我的視圖,以便可以直接進入生成的代碼並對其進行必要的更改,而不必自己重寫所有標記(這是為了節省時間)。

我嘗試進行此操作時會出現一些驗證錯誤,盡管如下圖所示,但出於質量考慮,我也會在此處引用它們。

這是我的ViewModel(我要腳手架安裝的)的樣子:

public class AddEditSupplierViewModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int ServiceId { get; set; }
    public IEnumerable<SelectListItem> Services { get; set; }
    public int SelectedRating { get; set; }
    public IEnumerable<SelectListItem> RatingList { get; set; }
    public string Address { get; set; }
    public string EmailAddress { get; set; }
    public string Telephone { get; set; }
    public double Commission { get; set; }
    public int PaymentTermId { get; set; }
    public IEnumerable<SelectListItem> PaymentTerms { get; set; }
    public bool Preferred { get; set; }
    public DateTime BEEExpiry { get; set; }
    public string SHEQ { get; set; }

    public AddEditSupplierViewModel()
    {
        var List = new List<int> { 1, 2, 3, 4, 5 };

        RatingList = List.Select(x => new SelectListItem
        {
            Value = x.ToString(),
            Text = x.ToString()
        });
    }
}

看起來錯誤繼續超出錯誤框的范圍,所以我不認為這是全部,但是看起來這與我的視圖模型無關。

我是從一個准MVC項目開始的。 我沒有做任何特別的事情。 剛剛將我的模型添加到ApplicationDbContext中,該模型根據每個ASP.NET Identity構建,就像我在幾個沒有這些問題的項目中所做的那樣。

有什么想法嗎? 我該如何擺脫這些並搭建我的觀點?

運行所選代碼生成器時發生錯誤:
'無法檢索以下數據的元數據
'SuppliersMVC.Models.AddEditSupplierViewModel'。 在模型生成期間檢測到一個或多個驗證錯誤:

異常::EntityType'Exception'沒有定義鍵。 為此實體類型定義一個鍵。

IdentityReference::EntityType'IdentityReference'尚未定義鍵。 為此實體類型定義一個鍵。

AssemblyName::EntityType'AssemblyName'未定義鍵。 定義此EntityType的鍵

DateTimeFormatInto::EntityType'DateTimeFormatInfo'沒有定義鍵。 定義此EntityType的鍵

異常:EntityType:EntitySet'異常基於未定義鍵的'異常'類型。

IdentityReferences:EntityType:EntitySet'IdentityReferences'基於沒有定義鍵的'IdentityReference'類型。

AssemblyNames:EntityType:EntitySet'AssemblyNames'基於未定義鍵的'AssemblyNames'類型。

CultureInfoes:EntityType:EntitySet'CultureInfoes'基於未定義鍵的'CultureInfo'類型。

DateTimeFormatInfoes:EntityType:EntitySet'DateTimeFormatInfoes'基於未定義鍵的'DateTimeFOrmatInfo'類型。

在此處輸入圖片說明

嘗試將類更改為:

 public class AddEditSupplierViewModel
 {
    [Key] // add key to Primary Key
    public int Id {get; set; }

    public string Name { get; set; }
    public int ServiceId { get; set; }
    public IEnumerable<SelectListItem> Services { get; set; }
    public int SelectedRating { get; set; }
    public IEnumerable<SelectListItem> RatingList { get; set; }
    public string Address { get; set; }
    public string EmailAddress { get; set; }
    public string Telephone { get; set; }
    public double Commission { get; set; }
    public int PaymentTermId { get; set; }
    public IEnumerable<SelectListItem> PaymentTerms { get; set; }
    public bool Preferred { get; set; }
    public DateTime BEEExpiry { get; set; }
    public string SHEQ { get; set; }

    public AddEditSupplierViewModel()
    {
        var List = new List<int> { 1, 2, 3, 4, 5 };

        RatingList = List.Select(x => new SelectListItem
        {
            Value = x.ToString(),
            Text = x.ToString()
        });
    }
}

注意:如果該屬性的名稱不是Id ,則需要向其添加[Key]屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM