Use string as type of DbSet like DbSet<string> in EF-CodeFirst

最后都变了- 提交于 2019-12-11 02:33:47

问题


I want to have a list of string and save and load them to/from database using EF-CodeFirst. this is my DbContext class:

public class KeysDbContext : DbContext
{
    public DbSet<string> Keys { get; set; }
}

but when I try to run the code I get this exception:System.InvalidOperationException : The type 'System.String' was not mapped. Check that the type has not been explicitly excluded by using the Ignore method or NotMappedAttribute data annotation. Verify that the type was defined as a class, is not primitive, nested or generic, and does not inherit from EntityObject.

How can I resolve the problem?


回答1:


You cannot have a DbSet<string>. String is treated as a primitive type by EF and the type for the DbSet has to be an entity. Entity has properties (which usually are mapped to columns in database) and also have to have keys. If, in your database you have a table that has just one string column you would have to create an entity with a string property to model this. In addition the string property will have to be a key property.



来源:https://stackoverflow.com/questions/13000748/use-string-as-type-of-dbset-like-dbsetstring-in-ef-codefirst

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!