使用字符串作为DbSet的类型,如EF-Code First </string>中的DbSet <string>

时间:2012-10-21 18:20:59

标签: c# entity-framework c#-4.0 ef-code-first

我想要一个字符串列表并使用EF-CodeFirst保存并从数据库加载它们。这是我的DbContext类:

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

但是当我尝试运行代码时,我遇到了这个例外:System.InvalidOperationExceptionThe 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.

如何解决问题?

1 个答案:

答案 0 :(得分:2)

你不能拥有DbSet<string>。字符串被EF视为基本类型,DbSet的类型必须是实体。实体具有属性(通常映射到数据库中的列)并且还必须具有键。如果在您的数据库中有一个只有一个字符串列的表,则必须创建一个带有字符串属性的实体来对其进行建模。此外,string属性必须是一个关键属性。

相关问题