当我启动我的MVC应用程序时,这个错误来自upp "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’。
我已经搜索了一些答案,所有人都说我必须向Model添加密钥,我做到了,那么这里发生了什么??
我使用的是Visual Studio Express 2013 for Web,以及所有最新版本的MVC和EF。
/Thanks
不过,这是可行的!
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; }
}发布于 2013-10-24 18:46:11
首先,不要在你的模型中使用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; }
}发布于 2013-10-24 18:54:50
您的对象关系映射(这里可能是EF )相信HttpPostedFile是数据库中的一个实体,而ImageData是一个导航属性。
当您在控制器中获得HttpPostedFile或HttpPostedFileBase时,您应该以byte[]的形式获取其内容,然后将其传递给Advert实体。这里有一个例子:http://msdn.microsoft.com/en-us/library/system.web.httppostedfile.inputstream(v=vs.110).aspx
https://stackoverflow.com/questions/19563743
复制相似问题