System.Drawing.Image EntityType“图像”没有定义键。定义此 EntityType 的键
我正在使用 EF Code First 和 Data Scaffolding,并且有一个包含类型属性的实体
public System.Drawing.Image Image { get; set; }
在初始化数据库上下文以创建数据库时出现以下错误:
System.Data.Edm.EdmEntityType:: EntityType“图像”没有定义键。 定义此 EntityType 的键。
System.Data.Edm.EdmEntitySet: EntityType: EntitySet Images 是基于 在没有键的类型图像上 已定义。
关于如何解决这个问题有什么想法吗?
I'm using EF Code First and Data Scaffolding and have an entity that contains a property of type
public System.Drawing.Image Image { get; set; }
When initialsing the DB context to create the DB i get the following errors:
System.Data.Edm.EdmEntityType: :
EntityType 'Image' has no key defined.
Define the key for this EntityType.System.Data.Edm.EdmEntitySet:
EntityType: EntitySet Images is based
on type Image that has no keys
defined.
Any ideas on how to get arround this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
按照 @Gats 的回答 - 您无法将所有类映射到 EF。 EF 仅理解基本类型,并且每个映射类必须被识别为实体或复杂类型。因此,您的
Impage
必须定义为:通过将其标记为
byte[]
EF 将了解它必须在 SQL Server 中存储为varbinary
。 EF 不支持自定义类型或自定义初始值设定项,因此您不能说 EF 您的Image
应该是其他任何内容。如果您还想将
Image
公开为 System.Drawing.Image,您可以执行以下操作:Following @Gats answer - you cannot map all classes to EF. EF understands only basic types and each mapped class must be either recognized as entity or complex type. So your
Impage
must be defined as:By marking it as
byte[]
EF will understand that it must be stored asvarbinary
in SQL server. EF doesn't support custom types or custom initializers so you cannot say EF that yourImage
should be anything else.If you want to expose
Image
as System.Drawing.Image as well you can do something like:这是因为 EF 无法跟踪它不可用的类(例如 System.Drawing.Image),并且它将您的“图像”属性视为它自己的实体,并期望它的映射信息(包括主键)。
如果您要在数据库中存储图像,则最好使用二进制属性,然后将实际的图像属性添加为只读非映射属性,以将二进制文件转换为图像文件。 System.Drawing.Image 不是只能映射到 SQL 的数据类型。
当您完成此操作并且想要阻止属性被映射时,请使用以下数据注释:
This is because EF cannot track a class that's not available to it (like System.Drawing.Image) and it sees your "image" property as being it's own entity and expects mapping information for it (including a primary key).
If you are storing an image in your database, you will be better off using a binary property and then adding the actual Image property as a read only non-mapped property that converts the binary to an image file. System.Drawing.Image is NOT a datatype that can just map to SQL.
When you have done that and you want to prevent a property from being mapped use the following data annotation: