public class Student
{
public int StuId { get; set; } // 不想当作主键
public string Name{ get; set; }
public string Addr{ get; set; }
} public class MyDBCatalog : DbContext
{
public DbSet<Student> Students { get; set; }
}}
问题一:
当程序运行时报错:Unable to infer a key for entity type "Test.Student'.
由于没有给StuId 加上key属性,所以报错,有没有办法能够做到,不添加主键或外键,使其能够正常使用,
网上有人说用EntityKey,但不知道怎么用。
问题二:
请问StuId 能否加上Identity(1,1),如何添加,多谢。
{
[Key]
public int Id{ get; set; } // 不想当作主键 public int StuId { get; set; } // 不想当作主键
public string Name{ get; set; }
public string Addr{ get; set; }
} 数据库中设置Identity(1,1),EF中好像没法设置。
类http://msdn.microsoft.com/en-us/library/bb156173.aspx
。问题2,貌似只能在语句或数据库中设置吧..
2. 如何能够加上Identity(1,1) 呢