使用“EF-Code First”时,我得到一个ModelValidationException(在底部).它希望我定义一个Key,但我不确定它究竟意味着什么……
public class Unit { Guid id; String public_id; String name; bool deleted; } public class MyDataContext : DbContext { public DbSet<Unit> Units { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Unit>().ToTable("soMetable"); } } [TestFixture] public class DataTests { [Test] public void test() { MyDataContext database = new MyDataContext(); var o = database.Units; Console.WriteLine(o.Count()); // This line throws! Assert.IsTrue(true); } }
System.Data.Entity.ModelConfiguration.ModelValidationException : One or more validation errors were detected during model generation:
System.Data.Edm.EdmEntityType: : EntityType ‘Unit’ has no key defined. Define the key for this EntityType.
System.Data.Edm.EdmEntitySet: EntityType: The EntitySet Units is based on type Unit that has no keys defined.