public void Test_Ef_Get_PK() { // Arrange DbContext db = new EfDbMapper(connectionString); var objectContext = ((System.Data.Entity.Infrastructure.IObjectContextAdapter)db).ObjectContext; System.Data.Objects.ObjectSet<Product> set = objectContext.CreateObjectSet<Product>(); //Act IEnumerable<string> keyNames = set.EntitySet.ElementType .KeyMembers .Select(k => k.Name); // Assert Assert.AreEqual("ProductId", string.Join(",", keyNames.ToArray())); }
public void Test_Ef_Get_Pk_via_pure_reflection() { // logic sourced here: http://stackoverflow.com/questions/7253943/entity-framework-code-first-find-primary-key // Arrange DbContext db = new EfDbMapper(connectionString); var objectContext = ((System.Data.Entity.Infrastructure.IObjectContextAdapter)db).ObjectContext; Type t = typeof(Product); // http://stackoverflow.com/a/232621 MethodInfo m = objectContext.GetType().GetMethod("CreateObjectSet", new Type[] { }); MethodInfo generic = m.MakeGenericMethod(t); object set = generic.Invoke(objectContext, null); PropertyInfo entitySetPI = set.GetType().GetProperty("EntitySet"); System.Data.Metadata.Edm.EntitySet entitySet = (System.Data.Metadata.Edm.EntitySet) entitySetPI.GetValue(set, null); // Act IEnumerable<string> keyNames = entitySet.ElementType.KeyMembers.Select(k => k.Name); // Assert Assert.AreEqual("ProductId", string.Join(",", keyNames.ToArray())); }
public void Ef_Can_save_transaction() { EmptyDatabase(); var x = new EfDbMapper(connectionString); ITransactionBoundFactory xf = new EF.TransactionBoundFactory(); IRepository<Product> prod = new EF.Repository<Product>(x); IRepository<Question> ques = new EF.Repository<Question>(x); Common_Can_save_transaction(xf, prod, ques); }
public void Test_Ef_Get_PK_via_dynamic() { // Arrange DbContext db = new EfDbMapper(connectionString); var objectContext = ((System.Data.Entity.Infrastructure.IObjectContextAdapter)db).ObjectContext; Type t = typeof(Product); // http://stackoverflow.com/a/232621 MethodInfo m = objectContext.GetType().GetMethod("CreateObjectSet",new Type[] {}); MethodInfo generic = m.MakeGenericMethod(t); dynamic set = generic.Invoke(objectContext, null); // Act var ks = (System.Data.Metadata.Edm.ReadOnlyMetadataCollection<System.Data.Metadata.Edm.EdmMember>)set.EntitySet.ElementType.KeyMembers; IEnumerable<string> keyNames = ks.Select(k => k.Name); // Assert Assert.AreEqual("ProductId", string.Join(",", keyNames.ToArray())); }