2013-10-24 47 views
0

當我開始我的MVC應用程序,這個錯誤出現UPP 「的EntityType‘HttpPostedFile’沒有定義鍵」的EntityType「HttpPostedFile」無鍵定義

有人能告訴我什麼是錯在這裏嗎?

型號:

public partial class Advert 
{ 
    [Key] 
    public int ID { get; set; } 

    [Required] 
    public HttpPostedFile ImageData { get; set; } 

    [Required] 
    public string UrlToUse { get; set; } 

    [Required] 
    public string Author { get; set; } 

    [Required] 
    public int SchemaType { get; set; } 

    public string Title { get; set; } 
} 

當控制器撞到,我運行這個

public ActionResult DisplayAdvert() 
    { 
     db.Database.CreateIfNotExists(); 

     return PartialView("_Advert"); 
    } 

和吊杆,在行db.Database.CreateIfNotExists();失敗:

Boat_Club.Models.HttpPostedFile:的EntityType 'HttpPostedFile' 沒有定義鍵。定義此EntityType的關鍵字。 HttpPostedFiles:EntityType:EntitySet'HttpPostedFiles'基於沒有定義鍵的類型'HttpPostedFile'。

我搜索了一些答案,所有說我要補充[關鍵]示範,而我,所以這是怎麼回事?

我使用Visual Studio Express的2013年的Web,用MVC和EF的所有latets版本。

/謝謝

雖然這工作!

public partial class Advert 
{ 
    [Key] 
    public int ID { get; set; } 

    [Required] 
    public byte[] ImageData { get; set; } 

    [Required] 
    public string UrlToUse { get; set; } 

    [Required] 
    public string Author { get; set; } 

    [Required] 
    public int SchemaType { get; set; } 

    public string Title { get; set; } 
} 

回答

0

首先不要在模型中使用HttpPostedFile,它不應該被序列化到任何地方。

取而代之的是將您的圖片數據聲明爲byte[],或者如果您需要更多詳細信息以及創建另一個類型來保存這些數據,則從發佈的文件實例中傳輸所需的詳細信息。

例如:

public partial class Advert 
{ 
    [Key] 
    public int ID { get; set; } 

    [Required] 
    public byte[] ImageData { get; set; } 

    [Required] 
    public string UrlToUse { get; set; } 

    [Required] 
    public string Author { get; set; } 

    [Required] 
    public int SchemaType { get; set; } 

    public string Title { get; set; } 
} 
+0

謝謝!剛剛嘗試過,它的工作原理!看到在這裏的另一篇文章,使用HttpPostedFileBase,這是可能的! [SO張貼在這裏(http://stackoverflow.com/questions/8777623/how-to-upload-image-to-sql-with-mvc3-within-one-form-using-ado-net) –

+0

不確定,但我個人仍然不會直接使用這些類型。 – Lloyd

+0

試過HttpPostedFileBase,那裏沒有運氣。所以我要用字節[],對我來說更有意義!謝謝!! –

相關問題