示例#1
0
        public static List <IProject> CreateProjects()
        {
            var ctx      = new MyEntityContext();
            var projects = new List <IProject>();
            var j        = 0;

            char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
            for (int i = 0; i < 260; i++)
            {
                var p = ctx.Projects.Create();
                p.Title = alpha[j] + "_Project_Name " + Guid.NewGuid().ToString();
                //p.Website = new Uri("http://data.gov.uk/");
                p.StartDate   = DateTime.Now;
                p.ProjectCode = 1727 + i;
                projects.Add(p);

                j++;
                if (j > 25)
                {
                    j = 0;
                }
            }

            ctx.SaveChanges();
            return(projects);
        }
        public void TestCreateAndRetrieve()
        {
            string storeName = MakeStoreName("TestCreateAndRetrieve", Configuration.PersistenceType);
            string personId;
            using (var dataObjectStore = _dataObjectContext.CreateStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    var person = context.Persons.Create();
                    Assert.IsNotNull(person);
                    context.SaveChanges();
                    Assert.IsNotNull(person.Id);
                    personId = person.Id;
                }
            }

            using (var dataObjectStore = _dataObjectContext.OpenStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    var person = context.Persons.FirstOrDefault(p => p.Id == personId);
                    Assert.IsNotNull(person);
                }
            }
        }
        protected static void InsertInBatches(int numBatches, int numPerBatch, string storeName)
        {
            var connectionString = String.Format("type=embedded;storesDirectory={0};storeName={1}",
                                                 TestConfiguration.StoreLocation, storeName);
            int docNum = 0;

            for (int batchNum = 0; batchNum < numBatches; batchNum++)
            {
                using (var context = new MyEntityContext(connectionString))
                {
                    var start = DateTime.Now;
                    Console.WriteLine("Building batch #{0}", batchNum);
                    for (int i = 0; i < numPerBatch; i++, docNum++)
                    {
                        var doc = context.Articles.Create();
                        doc.Title       = "Document #" + docNum;
                        doc.PublishDate = new DateTime(2000, 01, 01).AddDays(docNum % 5000);
                    }
                    var end = DateTime.Now;
                    Console.WriteLine("Building batch took {0}ms", end.Subtract(start).TotalMilliseconds);
                    Console.WriteLine("Saving batch {0}...", batchNum);
                    start = DateTime.Now;
                    context.SaveChanges();
                    end = DateTime.Now;
                    Console.WriteLine("Save completed in {0}ms", end.Subtract(start).TotalMilliseconds);
                }
            }
        }
        public void TestCreateAndRetrieve()
        {
            string storeName = Guid.NewGuid().ToString();
            string personId;
            using (var dataObjectStore = _dataObjectContext.CreateStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    var person = context.Persons.Create();
                    Assert.IsNotNull(person);
                    context.SaveChanges();
                    Assert.IsNotNull(person.Id);
                    personId = person.Id;
                }
            }

            using (var dataObjectStore = _dataObjectContext.OpenStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    var person = context.Persons.FirstOrDefault(p => p.Id == personId);
                    Assert.IsNotNull(person);
                }
            }
        }
示例#5
0
 private void CreateBatch(int batchNumber)
 {
     using (var context = new MyEntityContext(_storeConnectionString))
     {
         for (int i = 0; i < BatchSize; i++)
         {
             CreatePerson(context, (batchNumber * BatchSize) + i);
         }
         context.SaveChanges();
     }
 }
示例#6
0
        public void TestLoadAndQuery25K(PersistenceType persistenceType)
        {
            var       storeName   = MakeStoreName("TestLoadAndQuery10K", persistenceType);
            const int personCount = 25000;
            var       hugosCount  = personCount / TestDataLists.Firstnames.Count; // Estimated number of instances with name 'HUGO' we will generate

            using (var doStore = CreateStore(storeName, persistenceType))
            {
                using (var context = new MyEntityContext(doStore))
                {
                    var last10 = new IFoafPerson[10];
                    for (int i = 0; i < personCount; i++)
                    {
                        // Create an entity with properties set
                        var person = context.FoafPersons.Create();
                        person.Name         = TestDataLists.Firstnames[i % TestDataLists.Firstnames.Count];
                        person.Organisation = TestDataLists.Organizations[i % TestDataLists.Organizations.Count];
                        foreach (var friend in last10.Where(friend => friend != null))
                        {
                            person.Knows.Add(friend);
                        }
                        last10[i % 10] = person;
                    }
                    context.SaveChanges();

                    // Attempt a query using the same store handle
                    var hugos = context.FoafPersons.Where(p => p.Name.Equals("HUGO")).ToList();
                    Assert.IsTrue(hugos.Count >= hugosCount, "Expected at least {0} foafPerson instances returned with name HUGO", hugosCount);
                    hugosCount = hugos.Count; // Acual number may be one higher depending on where the iteration leaves off.
                }
            }

            // Try query with a new store context
            using (var doStore = OpenStore(storeName))
            {
                using (var context = new MyEntityContext(doStore))
                {
                    var hugos = context.FoafPersons.Where(p => p.Name.Equals("HUGO")).ToList();
                    Assert.AreEqual(hugosCount, hugos.Count, "Expected count of HUGOs to be unchanged when querying from a second context instance");
                }
            }

            // Try query with a completely new client session
            BrightstarDB.Client.BrightstarService.Shutdown();
            using (var doStore = OpenStore(storeName))
            {
                using (var context = new MyEntityContext(doStore))
                {
                    var hugos = context.FoafPersons.Where(p => p.Name.Equals("HUGO")).ToList();
                    Assert.AreEqual(hugosCount, hugos.Count, "Expected count of HUGOs to be unchanged when querying from a third context instance");
                }
            }
        }
        private void CreateBatch(int batchNumber)
        {
            using (var context = new MyEntityContext(_storeConnectionString))
            {
                for (int i = 0; i < BatchSize; i++)
                {

                    CreatePerson(context, (batchNumber*BatchSize) + i);
                }
                context.SaveChanges();
            }
        }
        public void TestCreateEntity()
        {
            var context = new MyEntityContext("type=embedded;storesdirectory=brightstar;storename=test-" + Guid.NewGuid());

            context.Notes.Add(new Note {
                Title = "My First Note"
            });
            context.SaveChanges();
            var allNotes = context.Notes.ToList();

            Assert.AreEqual(1, allNotes.Count());
        }
示例#9
0
        public void TestLoadAndQuery25K(PersistenceType persistenceType)
        {
            var storeName = MakeStoreName("TestLoadAndQuery10K", persistenceType);
            const int personCount = 25000;
            var hugosCount = personCount/TestDataLists.Firstnames.Count; // Estimated number of instances with name 'HUGO' we will generate
            using (var doStore = CreateStore(storeName, persistenceType))
            {
                using (var context = new MyEntityContext(doStore))
                {
                    var last10 = new IFoafPerson[10];
                    for (int i = 0; i < personCount; i++)
                    {
                        // Create an entity with properties set
                        var person = context.FoafPersons.Create();
                        person.Name = TestDataLists.Firstnames[i%TestDataLists.Firstnames.Count];
                        person.Organisation = TestDataLists.Organizations[i%TestDataLists.Organizations.Count];
                        foreach (var friend in last10.Where(friend => friend != null))
                        {
                            person.Knows.Add(friend);
                        }
                        last10[i%10] = person;
                    }
                    context.SaveChanges();

                    // Attempt a query using the same store handle
                    var hugos = context.FoafPersons.Where(p => p.Name.Equals("HUGO")).ToList();
                    Assert.IsTrue(hugos.Count >= hugosCount, "Expected at least {0} foafPerson instances returned with name HUGO", hugosCount);
                    hugosCount = hugos.Count; // Acual number may be one higher depending on where the iteration leaves off.
                }
            }

            // Try query with a new store context
            using (var doStore = OpenStore(storeName))
            {
                using (var context = new MyEntityContext(doStore))
                {
                    var hugos = context.FoafPersons.Where(p => p.Name.Equals("HUGO")).ToList();
                    Assert.AreEqual(hugosCount, hugos.Count, "Expected count of HUGOs to be unchanged when querying from a second context instance");
                }
            }

            // Try query with a completely new client session
            BrightstarDB.Client.BrightstarService.Shutdown();
            using (var doStore = OpenStore(storeName))
            {
                using (var context = new MyEntityContext(doStore))
                {
                    var hugos = context.FoafPersons.Where(p => p.Name.Equals("HUGO")).ToList();
                    Assert.AreEqual(hugosCount, hugos.Count, "Expected count of HUGOs to be unchanged when querying from a third context instance");
                }
            }
        }
        public HttpResponseMessage MarkLocation([FromBody] FormDataCollection formVars)
        {
            string user = formVars["user"];
            string type = formVars["type"];
            string location = formVars["location"];

            string connectionString = Constants.storeConnectionString;
            var client = BrightstarService.GetClient(Constants.storeConnectionString);

            ILocation markedLocation = JsonConvert.DeserializeObject<Location>(location);
            using (var ctx = new MyEntityContext(connectionString))
            {
                IUser usr = ctx.Users.Single(x => x.Email == user);

                if (!usr.MarkedLocations.Any(x => x.Equals(markedLocation)))
                {
                    var marker = ctx.Locations.Create();
                    marker.name = markedLocation.name;
                    marker.latitude = markedLocation.latitude;
                    marker.longitude = markedLocation.longitude;
                    marker.attributes = markedLocation.attributes;
                    if(type == "fav")
                        marker.IsFavorite = markedLocation.IsFavorite;
                    else
                        marker.IsVisited = markedLocation.IsVisited;
                    usr.MarkedLocations.Add(marker);
                }
                else
                {
                    //update existing location
                    ILocation existingLocation = usr.MarkedLocations.Single(x => x.Equals(markedLocation));
                    if (type == "fav")
                        existingLocation.IsFavorite = markedLocation.IsFavorite;
                    else
                        existingLocation.IsVisited = markedLocation.IsVisited;
                }
                ctx.SaveChanges();

                return new HttpResponseMessage(HttpStatusCode.OK);//200
            }
        }
示例#11
0
        public void TestCustomTriplesQuery()
        {
            string storeName = MakeStoreName("TestCustomTriplesQuiery", Configuration.PersistenceType);
            var people = new Person[10];
            using (var dataObjectStore = _dataObjectContext.CreateStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    for (int i = 0; i < 10; i++)
                    {
                        var person = new Person { Age = 40 - i, Name = "Person #" + i };
                        context.Persons.Add(person);
                        people[i] = person;
                    }
                    context.SaveChanges();
                }
            }


            using (var dataObjectStore = _dataObjectContext.OpenStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    const string query = @"
select  ?s ?p ?o
where {
   ?s ?p ?o.
        ?s a <http://www.example.org/schema/Person>
}
";
                    IList<Person> results = context.ExecuteQuery<Person>(query).ToList();
                    Assert.AreEqual(10, results.Count);

                    foreach (Person person in results)
                    {
                        Assert.AreNotEqual(0, person.Age);
                    }
                }
            }
        }
示例#12
0
 private static void SetupStore()
 {
     if (!Directory.Exists("brightstardb"))
     {
         Directory.CreateDirectory("brightstardb");
         using (var context = new MyEntityContext(ConnectionString)
                )
         {
             var doc1 = context.Documents.Create();
             var doc2 = context.Documents.Create();
             var doc3 = context.Documents.Create();
             var p1   = context.Principles.Create();
             p1.History = new List <string> {
                 "Strings", "of", "Text"
             };
             var p2 = context.Principles.Create();
             p2.History = new List <string> {
                 "Nothing", "To See Here", "Move Along"
             };
             var p3 = context.Principles.Create();
             p3.History = new List <string> {
                 "Another", "Principle containing Text"
             };
             doc1.Principles = new List <IPrinciple> {
                 p1
             };
             doc2.Principles = new List <IPrinciple> {
                 p2
             };
             doc3.Principles = new List <IPrinciple> {
                 p3
             };
             context.SaveChanges();
         }
     }
 }
示例#13
0
        public void TestCollectionUpdatedByInverseProperty(PersistenceType persistenceType)
        {
            var storeName = MakeStoreName("TestCollectionUpdatedByInverseProperty", persistenceType);
            using (var dataObjectStore = CreateStore(storeName, persistenceType))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    var dept = new Department {Name = "Research"};
                    context.Departments.Add(dept);

                    // Attach before property is set
                    var alice = new Person {Name = "Alice"};
                    context.Persons.Add(alice);
                    alice.Department = dept;
                    Assert.AreEqual(1, dept.Persons.Count);

                    // Attach after property set
                    var bob = new Person {Name = "Bob", Department = dept};
                    context.Persons.Add(bob);
                    Assert.AreEqual(2, dept.Persons.Count);

                    // Attach after property set by explicit call
                    var charlie = new Person {Name = "Charlie"};
                    charlie.Department = dept;
                    context.Persons.Add(charlie);
                    Assert.AreEqual(3, dept.Persons.Count);

                    // Not attached before checking inverse property
                    var dave = new Person {Name = "Dave", Department = dept};
                    Assert.AreEqual(3, dept.Persons.Count);
                    context.Persons.Add(dave);
                    Assert.AreEqual(4, dept.Persons.Count);

                    context.SaveChanges();

                    Assert.AreEqual(4, dept.Persons.Count);
                    context.DeleteObject(bob);
                    Assert.AreEqual(3, dept.Persons.Count);
                    context.SaveChanges();

                    Assert.AreEqual(3, dept.Persons.Count);
                }
            }
        }
示例#14
0
        public void TestSkipAndTake()
        {
            string storeName = Guid.NewGuid().ToString();
            var people = new Person[10];
            using (var dataObjectStore = _dataObjectContext.CreateStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    for (int i = 0; i < 10; i++)
                    {
                        var person = new Person { Age = 40 - i, Name = "Person #" + i };
                        context.Persons.Add(person);
                        people[i] = person;
                    }
                    context.SaveChanges();
                }
            }

            using (var dataObjectStore = _dataObjectContext.OpenStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {

                    // Take, skip and skip and take with no other query expression
                    var top3 = context.Persons.Take(3).ToList();
                    Assert.AreEqual(3, top3.Count);
                    foreach (var p in top3)
                    {
                        Assert.IsTrue(people.Any(x => p.Id.Equals(x.Id)));
                    }
                    var after3 = context.Persons.Skip(3).ToList();
                    Assert.AreEqual(7, after3.Count);
                    var nextPage = context.Persons.Skip(3).Take(3).ToList();
                    Assert.AreEqual(3, nextPage.Count);

                    // Combined with a sort expression
                    var top3ByAge = context.Persons.OrderByDescending(p => p.Age).Take(3).ToList();
                    Assert.AreEqual(3, top3ByAge.Count);
                    foreach (var p in top3ByAge) Assert.IsTrue(p.Age >= 38);

                    var allButThreeOldest = context.Persons.OrderByDescending(p => p.Age).Skip(3).ToList();
                    Assert.AreEqual(7, allButThreeOldest.Count);
                    foreach (var p in allButThreeOldest) Assert.IsFalse(p.Age >= 38);

                    var nextThreeOldest = context.Persons.OrderByDescending(p => p.Age).Skip(3).Take(3).ToList();
                    Assert.AreEqual(3, nextThreeOldest.Count);
                    foreach (var p in nextThreeOldest) Assert.IsTrue(p.Age < 38 && p.Age > 34);
                }
            }
        }
示例#15
0
        public void TestSetPropertiesThenAttach()
        {
            string storeName = Guid.NewGuid().ToString();
            using (var dataObjectStore = _dataObjectContext.CreateStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    // ReSharper disable UseObjectOrCollectionInitializer
                    // Purposefully setting properties and then attaching Context property
                    var person = new Person
                    {
                        Name = "Kal",
                        DateOfBirth = new DateTime(1970, 12, 12),
                        Friends = new List<IPerson>
                                {
                                    new Person {Name = "Gra", Id = "http://example.org/people/1234"},
                                    new Person {Name = "Stu", Id = "http://example.org/people/456"}
                                }
                    };
                    person.Id = "http://example.org/people/123";
                    person.Context = context;
                    // ReSharper restore UseObjectOrCollectionInitializer
                    context.SaveChanges();
                }
            }
            using (var dataObjectStore = _dataObjectContext.OpenStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    var found =
                        context.Persons.FirstOrDefault(p => p.Id.Equals("http://example.org/people/123"));
                    Assert.IsNotNull(found);
                    Assert.AreEqual("Kal", found.Name);
                    Assert.AreEqual(new DateTime(1970, 12, 12), found.DateOfBirth);

                    found = context.Persons.FirstOrDefault(p => p.Id.Equals("http://example.org/people/1234"));
                    Assert.IsNotNull(found);
                    Assert.AreEqual("Gra", found.Name);

                    found = context.Persons.FirstOrDefault(p => p.Id.Equals("http://example.org/people/456"));
                    Assert.IsNotNull(found);
                    Assert.AreEqual("Stu", found.Name);
                }
            }
        }
示例#16
0
        public void TestManyToOneInverse()
        {
            string storeName = Guid.NewGuid().ToString();
            string rootId, skillAId, skillBId, childSkillId, childSkill2Id;
            using (var dataObjectStore = _dataObjectContext.CreateStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {

                    var root = context.Skills.Create();
                    root.Name = "Root";
                    var skillA = context.Skills.Create();
                    skillA.Parent = root;
                    skillA.Name = "Skill A";
                    var skillB = context.Skills.Create();
                    skillB.Parent = root;
                    skillB.Name = "Skill B";

                    Assert.IsNotNull(root.Children);
                    Assert.AreEqual(2, root.Children.Count);
                    Assert.IsTrue(root.Children.Any(x => x.Id.Equals(skillA.Id)));
                    Assert.IsTrue(root.Children.Any(x => x.Id.Equals(skillB.Id)));
                    context.SaveChanges();

                    rootId = root.Id;
                    skillAId = skillA.Id;
                    skillBId = skillB.Id;
                }
            }

            using (var dataObjectStore = _dataObjectContext.OpenStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    var root = context.Skills.FirstOrDefault(x => x.Id.Equals(rootId));
                    Assert.IsNotNull(root);
                    var childSkill = context.Skills.Create();
                    childSkill.Name = "Child Skill";
                    childSkill.Parent = root;
                    Assert.AreEqual(3, root.Children.Count);
                    Assert.IsTrue(root.Children.Any(x => x.Id.Equals(childSkill.Id)));
                    Assert.IsTrue(root.Children.Any(x => x.Id.Equals(skillAId)));
                    Assert.IsTrue(root.Children.Any(x => x.Id.Equals(skillBId)));
                    context.SaveChanges();
                    childSkillId = childSkill.Id;
                }
            }

            using (var dataObjectStore = _dataObjectContext.OpenStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    var root = context.Skills.FirstOrDefault(x => x.Id.Equals(rootId));
                    Assert.IsNotNull(root);
                    Assert.AreEqual(3, root.Children.Count);
                    Assert.IsTrue(root.Children.Any(x => x.Id.Equals(childSkillId)));
                    Assert.IsTrue(root.Children.Any(x => x.Id.Equals(skillAId)));
                    Assert.IsTrue(root.Children.Any(x => x.Id.Equals(skillBId)));
                    var childSkill2 = context.Skills.Create();
                    childSkill2.Name = "Child Skill 2";
                    childSkill2.Parent = root;
                    Assert.AreEqual(4, root.Children.Count);
                    Assert.IsTrue(root.Children.Any(x => x.Id.Equals(childSkill2.Id)));
                    context.SaveChanges();
                    childSkill2Id = childSkill2.Id;
                }
            }

            using (var dataObjectStore = _dataObjectContext.OpenStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    var root = context.Skills.FirstOrDefault(x => x.Id.Equals(rootId));
                    var skillA = context.Skills.FirstOrDefault(x => x.Id.Equals(skillAId));
                    var childSkill2 = context.Skills.FirstOrDefault(x => x.Id.Equals(childSkill2Id));
                    Assert.IsNotNull(root);
                    Assert.IsNotNull(skillA);
                    Assert.IsNotNull(childSkill2);
                    Assert.AreEqual(4, root.Children.Count);
                    Assert.IsTrue(root.Children.Any(x => x.Id.Equals(childSkillId)));
                    Assert.IsTrue(root.Children.Any(x => x.Id.Equals(skillAId)));
                    Assert.IsTrue(root.Children.Any(x => x.Id.Equals(skillBId)));
                    Assert.IsTrue(root.Children.Any(x => x.Id.Equals(childSkill2Id)));
                    // Move a skill to a new parent
                    childSkill2.Parent = skillA;
                    Assert.AreEqual(3, root.Children.Count);
                    Assert.IsFalse(root.Children.Any(x => x.Id.Equals(childSkill2Id)));
                    Assert.AreEqual(1, skillA.Children.Count);
                    Assert.IsTrue(skillA.Children.Any(x => x.Id.Equals(childSkill2Id)));
                    context.SaveChanges();
                }
            }

            // Check the move has persisted
            using (var dataObjectStore = _dataObjectContext.OpenStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    var root = context.Skills.FirstOrDefault(x => x.Id.Equals(rootId));
                    var skillA = context.Skills.FirstOrDefault(x => x.Id.Equals(skillAId));
                    var childSkill2 = context.Skills.FirstOrDefault(x => x.Id.Equals(childSkill2Id));
                    Assert.IsNotNull(root);
                    Assert.IsNotNull(skillA);
                    Assert.IsNotNull(childSkill2);
                    Assert.AreEqual(3, root.Children.Count);
                    Assert.IsFalse(root.Children.Any(x => x.Id.Equals(childSkill2Id)));
                    Assert.AreEqual(1, skillA.Children.Count);
                    Assert.IsTrue(skillA.Children.Any(x => x.Id.Equals(childSkill2Id)));
                    Assert.AreEqual(skillA.Id, childSkill2.Parent.Id);
                    Assert.AreEqual(root.Id, childSkill2.Parent.Parent.Id);
                }
            }
        }
示例#17
0
        public void TestOneToOneInverse()
        {
            string storeName = Guid.NewGuid().ToString();
            string aliceId, bobId, carolId;
            using (var dataObjectStore = _dataObjectContext.CreateStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    var alice = context.Persons.Create();
                    alice.Name = "Alice";
                    var bob = context.Animals.Create();
                    alice.Pet = bob;
                    context.SaveChanges();
                    aliceId = alice.Id;
                    bobId = bob.Id;
                }
            }

            // See if we can access the inverse property
            using (var dataObjectStore = _dataObjectContext.OpenStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    var bob = context.Animals.FirstOrDefault(a => a.Id.Equals(bobId));
                    Assert.IsNotNull(bob);
                    Assert.IsNotNull(bob.Owner);
                    Assert.AreEqual(aliceId, bob.Owner.Id);

                    // see if we can access an item by querying against its inverse property
                    bob = context.Animals.FirstOrDefault(a => a.Owner.Id.Equals(aliceId));
                    Assert.IsNotNull(bob);

                    // check that alice.Pet refers to the same object as bob
                    bob.Name = "Bob";
                    Assert.IsNotNull(bob.Name);
                    Assert.AreEqual("Bob", bob.Name);
                    var alice = context.Persons.FirstOrDefault(a => a.Id.Equals(aliceId));
                    Assert.IsNotNull(alice);
                    var alicePet = alice.Pet;
                    Assert.IsNotNull(alicePet);
                    Assert.AreEqual(bob, alicePet);
                    Assert.AreEqual("Bob", alicePet.Name);

                    // Transfer object by changing the forward property
                    var carol = context.Persons.Create();
                    carol.Name = "Carol";
                    carol.Pet = bob;
                    carolId = carol.Id;
                    Assert.AreEqual(carol, bob.Owner);
                    Assert.IsNull(alice.Pet, "Expected alice.Pet to be null after owner change");
                    context.SaveChanges();
                }
            }

            // Check that changes to forward properties get persisted
            using (var dataObjectStore = _dataObjectContext.OpenStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    var bob = context.Animals.FirstOrDefault(a => a.Owner.Id.Equals(carolId));
                    Assert.IsNotNull(bob);
                    Assert.AreEqual("Bob", bob.Name);
                    var alice = context.Persons.FirstOrDefault(p => p.Id.Equals(aliceId));
                    Assert.IsNotNull(alice);
                    Assert.IsNull(alice.Pet);
                    var carol = context.Persons.FirstOrDefault(p => p.Id.Equals(carolId));
                    Assert.IsNotNull(carol);
                    Assert.IsNotNull(carol.Pet);
                    Assert.AreEqual(bob, carol.Pet);

                    // Transfer object by changing inverse property
                    bob.Owner = alice;
                    Assert.IsNotNull(alice.Pet);
                    Assert.IsNull(carol.Pet);
                    context.SaveChanges();
                }
            }

            // Check that changes to inverse properties get persisted
            using (var dataObjectStore = _dataObjectContext.OpenStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    var bob = context.Animals.FirstOrDefault(a => a.Id.Equals(bobId));
                    Assert.IsNotNull(bob);
                    Assert.AreEqual("Bob", bob.Name);
                    Assert.AreEqual(aliceId, bob.Owner.Id);
                    var alice = context.Persons.FirstOrDefault(p => p.Id.Equals(aliceId));
                    Assert.IsNotNull(alice);
                    Assert.IsNotNull(alice.Pet);
                    Assert.AreEqual(bob, alice.Pet);
                    var carol = context.Persons.FirstOrDefault(p => p.Id.Equals(carolId));
                    Assert.IsNotNull(carol);
                    Assert.IsNull(carol.Pet);
                }
            }
        }
示例#18
0
        public void TestSetRelatedEntitiesToNullThrowsArgumentNullException()
        {
            var storeName = "TestSetRelatedEntitiesToNullThrowsArgumentNullException_" + DateTime.Now.Ticks;
            string aliceId;
            using (var dataObjectStore = _dataObjectContext.CreateStore(storeName))
            {
                var context = new MyEntityContext(dataObjectStore);
                var alice = context.Persons.Create();
                var bob = context.Persons.Create();
                var carol = context.Persons.Create();
                alice.Friends = new List<IPerson> { bob, carol };
                context.SaveChanges();
                aliceId = alice.Id;
            }

            using (var dataObjectStore = _dataObjectContext.OpenStore(storeName))
            {
                var context = new MyEntityContext(dataObjectStore);
                var alice = context.Persons.FirstOrDefault(p => p.Id.Equals(aliceId));
                Assert.IsNotNull(alice);
                Assert.AreEqual(2, alice.Friends.Count);

                alice.Friends = null; // throws
            }
        }
示例#19
0
        public void TestPopulateEntityCollectionWithExistingEntities()
        {
            string storeName = Guid.NewGuid().ToString();
            string aliceId, bobId, carolId;
            using (var dataObjectStore = _dataObjectContext.CreateStore(storeName))
            {
                var context = new MyEntityContext(dataObjectStore);
                var alice = context.Persons.Create();
                alice.Name = "Alice";
                var bob = context.Persons.Create();
                bob.Name = "Bob";
                var carol = context.Persons.Create();
                carol.Name = "Carol";
                alice.Friends.Add(bob);
                alice.Friends.Add(carol);
                context.SaveChanges();
                aliceId = alice.Id;
                bobId = bob.Id;
                carolId = carol.Id;
            }

            // See if we can access the collection on a loaded object
            using (var dataObjectStore = _dataObjectContext.OpenStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    var alice = context.Persons.FirstOrDefault(p => p.Id == aliceId);
                    Assert.IsNotNull(alice);
                    var friends = alice.Friends as IEntityCollection<IPerson>;
                    Assert.IsNotNull(friends);
                    Assert.IsFalse(friends.IsLoaded);
                    friends.Load();
                    Assert.IsTrue(friends.IsLoaded);
                    Assert.AreEqual(2, alice.Friends.Count);
                    Assert.IsTrue(alice.Friends.Any(p => p.Id.Equals(bobId)));
                    Assert.IsTrue(alice.Friends.Any(p => p.Id.Equals(carolId)));
                }
            }
        }
示例#20
0
        private static void CreateData(MyEntityContext ctx)
        {
            //skills
            var skills = new List <Skill>();

            for (int i = 0; i < 1000; i++)
            {
                Skill s = ctx.Skills.Create();
                s.Title = "Skill" + i;
                skills.Add(s);
            }
            ctx.SaveChanges();

            //persons
            var people = new List <Person>();

            for (int i = 0; i < 1000; i++)
            {
                Person p = ctx.Persons.Create();
                p.Fullname       = "Person" + i;
                p.Salary         = 270000 + (i * 200);
                p.DateOfBirth    = DateTime.Now;
                p.EmployeeNumber = i;
                p.Age            = 20 + (i / 20);
                p.EmployeeNumber = i;
                Skill s = skills[i];
                p.Skills.Add(s);
                people.Add(p);
            }

            ctx.SaveChanges();

            //departments
            var depts = new List <Department>();

            for (int i = 0; i < 100; i++)
            {
                Department dep = ctx.Departments.Create();
                dep.Name   = "Department" + i;
                dep.DeptId = i;
                depts.Add(dep);
            }
            ctx.SaveChanges();

            var roles = new List <JobRole>();
            //Create Roles
            JobRole jobRoledevelopment = ctx.JobRoles.Create();

            jobRoledevelopment.Description = "development";
            roles.Add(jobRoledevelopment);

            JobRole jobRolesales = ctx.JobRoles.Create();

            jobRolesales.Description = "sales";
            roles.Add(jobRolesales);

            JobRole jobRolemarketing = ctx.JobRoles.Create();

            jobRolemarketing.Description = "marketing";
            roles.Add(jobRolemarketing);

            JobRole jobRolemanagement = ctx.JobRoles.Create();

            jobRolemanagement.Description = "management";
            roles.Add(jobRolemanagement);

            JobRole jobRoleadministration = ctx.JobRoles.Create();

            jobRoleadministration.Description = "administration";
            roles.Add(jobRoleadministration);

            ctx.SaveChanges();

            #region depts
            //100 employees per department
            int e = 0; int d = 0;
            while (e < 1000)
            {
                Department department = depts.Where(de => de.DeptId == d).First();

                var p = people.Where(pe => pe.EmployeeNumber == e).First();
                p.Department = department;
                p.JobRole    = roles[0];
                e            = e + 1;

                p            = people.Where(pe => pe.EmployeeNumber == e).First();
                p.Department = department;
                p.JobRole    = roles[0];
                e            = e + 1;

                p            = people.Where(pe => pe.EmployeeNumber == e).First();
                p.Department = department;
                p.JobRole    = roles[1];
                e            = e + 1;

                p            = people.Where(pe => pe.EmployeeNumber == e).First();
                p.Department = department;
                p.JobRole    = roles[1];
                e            = e + 1;

                p            = people.Where(pe => pe.EmployeeNumber == e).First();
                p.Department = department;
                p.JobRole    = roles[2];
                e            = e + 1;

                p            = people.Where(pe => pe.EmployeeNumber == e).First();
                p.Department = department;
                p.JobRole    = roles[2];
                e            = e + 1;

                p            = people.Where(pe => pe.EmployeeNumber == e).First();
                p.Department = department;
                p.JobRole    = roles[3];
                e            = e + 1;

                p            = people.Where(pe => pe.EmployeeNumber == e).First();
                p.Department = department;
                p.JobRole    = roles[3];
                e            = e + 1;

                p            = people.Where(pe => pe.EmployeeNumber == e).First();
                p.Department = department;
                p.JobRole    = roles[4];
                e            = e + 1;

                p            = people.Where(pe => pe.EmployeeNumber == e).First();
                p.Department = department;
                p.JobRole    = roles[4];
                e            = e + 1;

                d = d + 1;
            }
            ctx.SaveChanges();

            #endregion

            Website website1 = ctx.Websites.Create();
            website1.Name = "website1";
            website1.Url  = "http://website1.com";

            Website website2 = ctx.Websites.Create();
            website2.Name = "website2";
            website2.Url  = "http://website2.com";

            ctx.SaveChanges();

            #region articles
            //articles
            for (int i = 0; i < 10000; i++)
            {
                Article art = ctx.Articles.Create();
                art.Title    = "Article" + i;
                art.BodyText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis feugiat eleifend tempus. Maecenas mattis luctus volutpat. Morbi id felis diam. Morbi pulvinar tortor id nisl sagittis at bibendum turpis volutpat. Nulla gravida, elit et lobortis imperdiet, nulla sapien semper elit, eget posuere ipsum libero ac mi. Phasellus in risus nisi, sed aliquet nunc. Nunc auctor, justo non tristique dictum, lacus ligula pulvinar mauris, a sagittis lacus lectus id quam. Vivamus eget ante elit. Phasellus at hendrerit nunc. Vestibulum ac vehicula neque. Suspendisse ullamcorper scelerisque erat, non lacinia purus pretium quis. Praesent lacinia pellentesque ante id faucibus. Nunc eu enim eget erat convallis pulvinar. Curabitur ornare nisi dapibus massa sagittis non dictum augue pellentesque.Aliquam auctor, libero eu lobortis adipiscing, lorem nunc varius ante, at auctor sapien nunc sed augue. Phasellus ac leo nibh. Vivamus elit odio, accumsan at semper et, vestibulum a magna. Fusce pretium massa sed velit rutrum elementum. Cras ut dui quis elit gravida cursus. Aliquam tempor, nunc vel aliquam gravida, tortor urna aliquam eros, sed venenatis purus libero quis nisl. Quisque vulputate ultricies mi, nec lacinia tellus vulputate eu. Sed laoreet lacinia erat vitae auctor. Duis posuere dictum gravida. Morbi urna felis, rutrum volutpat tempor eget, porttitor at diam. Cras at urna diam. Ut nisl lorem, bibendum sed aliquet a, suscipit at lorem. In scelerisque, lorem ac tincidunt ornare, neque diam volutpat purus, eleifend ultricies odio erat eu ligula. Cras diam nisl, porttitor at commodo vitae, blandit semper ipsum. Nam tempor mattis lacus ac tempor.Aliquam erat volutpat. Praesent sed ligula diam, non tristique metus. Duis accumsan est nec quam ullamcorper eget vestibulum ligula facilisis. Sed malesuada lectus sit amet arcu sollicitudin tempor. Nam quis leo massa. Sed feugiat odio vel neque dictum venenatis. Mauris fermentum egestas erat nec semper. Aliquam dolor est, viverra ac egestas in, egestas vitae ligula. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae;Vestibulum tempor mi et erat eleifend sit amet rhoncus enim dictum. Mauris ultricies tortor vitae orci porttitor convallis lacinia dui auctor. Nulla facilisi. Nulla ligula urna, pharetra vel lacinia vitae, blandit id nunc. Morbi sed lacus id turpis mollis tempus. Suspendisse scelerisque elit id lorem interdum non pellentesque nulla laoreet. Suspendisse id ipsum libero. Duis rhoncus tincidunt libero a feugiat. Nunc tempus mauris venenatis justo dapibus in ultrices tortor euismod. Vestibulum pellentesque ante eget purus pretium at congue erat iaculis. Duis purus urna, placerat ac luctus et, luctus et tortor. Donec eu felis purus.Nunc sed blandit mauris. Aliquam sit amet neque velit, eget consequat nisi. Nunc facilisis, ante et bibendum porta, odio libero facilisis nunc, eget hendrerit lectus augue scelerisque ante. Maecenas fermentum dictum mollis. Morbi scelerisque urna sed quam tincidunt vehicula. Sed pharetra venenatis tellus sed laoreet. Integer ante libero, placerat vitae dignissim sed, eleifend ac erat. Sed eleifend viverra justo, vitae fringilla felis semper sed. Nam iaculis sagittis augue, non blandit arcu viverra vel. Phasellus tellus felis, dictum vel imperdiet vitae, aliquet a erat. Sed tempor suscipit condimentum.";
                var p = people.Where(guy => guy.EmployeeNumber == (i / 10)).SingleOrDefault();

                art.Publisher = p;
                if ((i % 2) == 0)
                {
                    art.Website = website1;
                }
                else
                {
                    art.Website = website2;
                }
            }

            ctx.SaveChanges();
            #endregion
        }
示例#21
0
        public void TestOneToManyInverse()
        {
            string storeName = MakeStoreName("TestOneToManyInverse", Configuration.PersistenceType);
            var connectionString = "type=embedded;storesDirectory=" + TestConfiguration.StoreLocation + ";storeName=" +
                                   storeName;
            string market1Id, market2Id, companyAId, companyBId, companyCId, companyDId;
            using (var context = new MyEntityContext(connectionString))
            {

                var market1 = context.Markets.Create();
                var market2 = context.Markets.Create();
                market1.Name = "Market1";
                market2.Name = "Market2";
                var companyA = context.Companies.Create();
                var companyB = context.Companies.Create();
                var companyC = context.Companies.Create();
                companyA.Name = "CompanyA";
                companyB.Name = "CompanyB";
                companyC.Name = "CompanyC";

                market1Id = market1.Id;
                market2Id = market2.Id;
                companyAId = companyA.Id;
                companyBId = companyB.Id;
                companyCId = companyC.Id;

                market1.ListedCompanies.Add(companyA);
                market2.ListedCompanies.Add(companyB);

                Assert.AreEqual(market1, companyA.ListedOn);
                Assert.AreEqual(market2, companyB.ListedOn);
                Assert.IsNull(companyC.ListedOn);
                context.SaveChanges();
            }
            using (var context = new MyEntityContext(connectionString))
            {
                var market1 = context.Markets.FirstOrDefault(x => x.Id.Equals(market1Id));
                var market2 = context.Markets.FirstOrDefault(x => x.Id.Equals(market2Id));
                var companyA = context.Companies.FirstOrDefault(x => x.Id.Equals(companyAId));
                var companyB = context.Companies.FirstOrDefault(x => x.Id.Equals(companyBId));
                var companyC = context.Companies.FirstOrDefault(x => x.Id.Equals(companyCId));
                Assert.IsNotNull(market1);
                Assert.IsNotNull(market2);
                Assert.IsNotNull(companyA);
                Assert.IsNotNull(companyB);
                Assert.IsNotNull(companyC);
                Assert.AreEqual(market1, companyA.ListedOn);
                Assert.AreEqual(market2, companyB.ListedOn);
                Assert.IsNull(companyC.ListedOn);

                // Add item to collection
                market1.ListedCompanies.Add(companyC);

                Assert.AreEqual(market1, companyA.ListedOn);
                Assert.AreEqual(market1, companyC.ListedOn);
                Assert.AreEqual(2, market1.ListedCompanies.Count);
                Assert.IsTrue(market1.ListedCompanies.Any(x => x.Id.Equals(companyA.Id)));
                Assert.IsTrue(market1.ListedCompanies.Any(x => x.Id.Equals(companyC.Id)));
                context.SaveChanges();
            }

            using (var context = new MyEntityContext(connectionString))
            {
                var market1 = context.Markets.FirstOrDefault(x => x.Id.Equals(market1Id));
                var market2 = context.Markets.FirstOrDefault(x => x.Id.Equals(market2Id));
                var companyA = context.Companies.FirstOrDefault(x => x.Id.Equals(companyAId));
                var companyB = context.Companies.FirstOrDefault(x => x.Id.Equals(companyBId));
                var companyC = context.Companies.FirstOrDefault(x => x.Id.Equals(companyCId));
                Assert.IsNotNull(market1);
                Assert.IsNotNull(market2);
                Assert.IsNotNull(companyA);
                Assert.IsNotNull(companyB);
                Assert.IsNotNull(companyC);

                Assert.AreEqual(market1, companyA.ListedOn);
                Assert.AreEqual(market1, companyC.ListedOn);
                Assert.AreEqual(2, market1.ListedCompanies.Count);
                Assert.IsTrue(market1.ListedCompanies.Any(x => x.Id.Equals(companyA.Id)));
                Assert.IsTrue(market1.ListedCompanies.Any(x => x.Id.Equals(companyC.Id)));

                // Set the single-valued inverse property
                var companyD = context.Companies.Create();
                companyD.Name = "CompanyD";
                companyD.ListedOn = market2;
                companyDId = companyD.Id;

                Assert.AreEqual(market2, companyB.ListedOn);
                Assert.AreEqual(market2, companyD.ListedOn);
                Assert.AreEqual(2, market2.ListedCompanies.Count);
                Assert.IsTrue(market2.ListedCompanies.Any(x => x.Id.Equals(companyB.Id)));
                Assert.IsTrue(market2.ListedCompanies.Any(x => x.Id.Equals(companyD.Id)));
                context.SaveChanges();
            }
            using (var context = new MyEntityContext(connectionString))
            {
                var market2 = context.Markets.FirstOrDefault(x => x.Id.Equals(market2Id));
                var companyB = context.Companies.FirstOrDefault(x => x.Id.Equals(companyBId));
                var companyD = context.Companies.FirstOrDefault(x => x.Id.Equals(companyDId));
                Assert.IsNotNull(market2);
                Assert.IsNotNull(companyB);
                Assert.IsNotNull(companyD);
                Assert.AreEqual(market2, companyB.ListedOn);
                Assert.AreEqual(market2, companyD.ListedOn);
                Assert.AreEqual(2, market2.ListedCompanies.Count);
                Assert.IsTrue(market2.ListedCompanies.Any(x => x.Id.Equals(companyB.Id)));
                Assert.IsTrue(market2.ListedCompanies.Any(x => x.Id.Equals(companyD.Id)));
            }
        }
示例#22
0
        public void TestSetRelatedEntitiesToNullThrowsArgumentNullException()
        {
            var storeName = MakeStoreName("TestSetRelatedEntitiesToNullThrowsArgumentNullException", Configuration.PersistenceType);
            string aliceId;
            using (var dataObjectStore = _dataObjectContext.CreateStore(storeName))
            {
                var context = new MyEntityContext(dataObjectStore);
                var alice = context.Persons.Create();
                var bob = context.Persons.Create();
                var carol = context.Persons.Create();
                alice.Friends = new List<IPerson> { bob, carol };
                context.SaveChanges();
                aliceId = alice.Id;
            }

            using (var dataObjectStore = _dataObjectContext.OpenStore(storeName))
            {
                var context = new MyEntityContext(dataObjectStore);
                var alice = context.Persons.FirstOrDefault(p => p.Id.Equals(aliceId));
                Assert.IsNotNull(alice);
                Assert.AreEqual(2, alice.Friends.Count);
                try
                {
                    alice.Friends = null; // throws
                    Assert.Fail("Expected ArgumentNullException to be thrown");
                }
                catch (ArgumentNullException)
                {
                    // Expected exception
                }
            }
        }
示例#23
0
        //connection string is in app.config
        public static void CreateData()
        {
            var ctx = new MyEntityContext();

            var dateFormedBase = new DateTime(2000, 01, 01, 12, 0, 0);
            var biblosFormed   =
                dateFormedBase.AddYears(1).AddMonths(1).AddDays(1).AddHours(1).AddMinutes(15).AddSeconds(15); //2nd feb 2001 13:15:15pm
            var hbFormed =
                biblosFormed.AddYears(1).AddMonths(1).AddDays(1).AddHours(1).AddMinutes(15).AddSeconds(15);   //3rd mar 2002 14:30:30pm

            //add companies
            var networkedPlanet = ctx.Companies.Create();

            networkedPlanet.Name        = "Networked Planet";
            networkedPlanet.Address     = "Oxford, UK";
            networkedPlanet.DateFormed  = dateFormedBase;
            networkedPlanet.SomeDecimal = 32.3800M;
            networkedPlanet.SomeDouble  = 32.3800;

            var company2 = ctx.Companies.Create();

            company2.Name        = "Biblos";
            company2.Address     = "Stokes Croft, Bristol, UK";
            company2.DateFormed  = biblosFormed;
            company2.SomeDecimal = 33.3800M;
            company2.SomeDouble  = 33.3800;

            var company3 = ctx.Companies.Create();

            company3.Name        = "Harry Blades";
            company3.Address     = "Christmas Steps, Bristol, UK";
            company3.DateFormed  = hbFormed;
            company3.SomeDecimal = 34.3800M;
            company3.SomeDouble  = 34.3800;

            ctx.SaveChanges();

            //skills

            for (int i = 0; i < 10; i++)
            {
                var s = ctx.Skills.Create();
                s.Name = "Skill" + i;
                Skills.Add(s);
            }
            ctx.SaveChanges();

            //add some child/parent skill relationships
            //first skill has 5 children, so each of those will have a parent. Leaves 5 skills as standalone
            var parentskill = Skills[0];

            foreach (var skill in ctx.Skills)
            {
                if (skill.Id.Equals(parentskill.Id))
                {
                    continue;
                }
                parentskill.Children.Add(skill);
            }
            ctx.SaveChanges();

            //persons
            var people = new List <IPerson>();

            for (int i = 0; i < 10; i++)
            {
                var p = ctx.Persons.Create();
                p.Name                = "Person" + i;
                p.Salary              = 27000 + (i * 400);
                p.DateOfBirth         = DateTime.Now;
                p.EmployeeId          = i;
                p.Age                 = 20 + (i / 20);
                p.CollectionOfStrings = new string[] { i.ToString(), i + 1.ToString(), i + 2.ToString() };
                var s = Skills[i];
                p.Skills.Add(s);
                people.Add(p);
            }

            ctx.SaveChanges();

            //departments
            var depts = new List <IDepartment>();

            for (int i = 0; i < 2; i++)
            {
                var dep = ctx.Departments.Create();
                dep.Name   = "Department" + i;
                dep.DeptId = i;
                depts.Add(dep);
            }
            ctx.SaveChanges();

            var roles = new List <IJobRole>();
            //Create Roles
            var jobRoledevelopment = ctx.JobRoles.Create();

            jobRoledevelopment.Description = "development";
            roles.Add(jobRoledevelopment);

            var jobRolesales = ctx.JobRoles.Create();

            jobRolesales.Description = "sales";
            roles.Add(jobRolesales);

            var jobRolemarketing = ctx.JobRoles.Create();

            jobRolemarketing.Description = "marketing";
            roles.Add(jobRolemarketing);

            var jobRolemanagement = ctx.JobRoles.Create();

            jobRolemanagement.Description = "management";
            roles.Add(jobRolemanagement);

            var jobRoleadministration = ctx.JobRoles.Create();

            jobRoleadministration.Description = "administration";
            roles.Add(jobRoleadministration);

            ctx.SaveChanges();

            #region depts
            //5 employees per department
            int e = 0; int d = 0;
            var peopleLookups = new List <double>();
            while (e < 10)
            {
                var department = depts.Where(de => de.DeptId == d).First();

                var p = people.Where(pe => pe.EmployeeId == e).First();
                p.Department = department;
                p.JobRole    = roles[0];
                e            = e + 1;

                p            = people.Where(pe => pe.EmployeeId == e).First();
                p.Department = department;
                p.JobRole    = roles[1];
                e            = e + 1;

                p            = people.Where(pe => pe.EmployeeId == e).First();
                p.Department = department;
                p.JobRole    = roles[2];
                e            = e + 1;

                p            = people.Where(pe => pe.EmployeeId == e).First();
                p.Department = department;
                p.JobRole    = roles[3];
                e            = e + 1;

                p            = people.Where(pe => pe.EmployeeId == e).First();
                p.Department = department;
                p.JobRole    = roles[4];
                e            = e + 1;

                d = d + 1;
            }
            ctx.SaveChanges();

            #endregion

            //Website website1 = ctx.Websites.Create();
            //website1.Name = "website1";
            //website1.URL = "http://website1.com";

            //Website website2 = ctx.Websites.Create();
            //website2.Name = "website2";
            //website2.URL = "http://website2.com";

            //ctx.SaveChanges();

            #region articles
            //articles
            for (int i = 0; i < 100; i++)
            {
                var art = ctx.Articles.Create();
                art.Title    = "Article" + i;
                art.BodyText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis feugiat eleifend tempus. Maecenas mattis luctus volutpat. Morbi id felis diam. Morbi pulvinar tortor id nisl sagittis at bibendum turpis volutpat. Nulla gravida, elit et lobortis imperdiet, nulla sapien semper elit, eget posuere ipsum libero ac mi. Phasellus in risus nisi, sed aliquet nunc. Nunc auctor, justo non tristique dictum, lacus ligula pulvinar mauris, a sagittis lacus lectus id quam. Vivamus eget ante elit. Phasellus at hendrerit nunc. Vestibulum ac vehicula neque. Suspendisse ullamcorper scelerisque erat, non lacinia purus pretium quis. Praesent lacinia pellentesque ante id faucibus. Nunc eu enim eget erat convallis pulvinar. Curabitur ornare nisi dapibus massa sagittis non dictum augue pellentesque.Aliquam auctor, libero eu lobortis adipiscing, lorem nunc varius ante, at auctor sapien nunc sed augue. Phasellus ac leo nibh. Vivamus elit odio, accumsan at semper et, vestibulum a magna. Fusce pretiu";
                var p = people.Where(guy => guy.EmployeeId == (i / 10)).SingleOrDefault();
                //var p = people.Where(guy => guy.EmployeeNumber == i).SingleOrDefault();

                art.Publisher = p;
                //if ((i % 2) == 0)
                //{
                //    art.Website = website1;
                //}
                //else
                //{
                //    art.Website = website2;
                //}
            }

            ctx.SaveChanges();
            #endregion

            CreateDataTypeTestData();
            CreateProjects();
        }
示例#24
0
        public void TestLoopThroughEntities()
        {
            string storeName = Guid.NewGuid().ToString();
            string homerId, bartId;
            using (var dataObjectStore = _dataObjectContext.CreateStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    var bart = context.Persons.Create();
                    bart.Name = "Bart Simpson";
                    var homer = context.Persons.Create();
                    homer.Name = "Homer Simpson";
                    bart.Father = homer;

                    var marge = context.Persons.Create();
                    marge.Name = "Marge Simpson";
                    bart.Mother = marge;

                    context.SaveChanges();
                    homerId = homer.Id;
                    bartId = bart.Id;
                }
            }

            // Query with results converted to a list
            using (var dataObjectStore = _dataObjectContext.OpenStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    var homersKids = context.Persons.Where(p => p.Father.Id == homerId).ToList();
                    Assert.AreEqual(1, homersKids.Count, "Could not find Bart with SPARQL query for Homer's kids");
                    Assert.AreEqual(bartId, homersKids.First().Id);
                }
            }
        }
示例#25
0
        public void TestSetGuid()
        {
            var storeName = "SetGuid_" + DateTime.Now.Ticks;
            var testGuid = new Guid(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
            using (var doStore = _dataObjectContext.CreateStore(storeName))
            {
                using (var context = new MyEntityContext(doStore))
                {
                    var testEntity = context.Entities.Create();
                    testEntity.SomeGuid = testGuid;
                    context.SaveChanges();
                }
            }
            using (var doStore = _dataObjectContext.OpenStore(storeName))
            {
                using (var context = new MyEntityContext(doStore))
                {
                    var testEntity = context.Entities.FirstOrDefault();
                    Assert.IsNotNull(testEntity);
                    var testEntityId = testEntity.Id;
                    Assert.AreEqual(testGuid, testEntity.SomeGuid);

                    // Verify we can use a Guid value in a search
                    testEntity = context.Entities.FirstOrDefault(e => e.SomeGuid.Equals(testGuid));
                    Assert.IsNotNull(testEntity);
                    Assert.AreEqual(testEntityId, testEntity.Id);
                    Assert.IsNull(context.Entities.FirstOrDefault(e => e.SomeGuid.Equals(Guid.Empty)));
                }
            }
        }
示例#26
0
        public void TestSetAndGetSingleRelatedObject()
        {
            string storeName = Guid.NewGuid().ToString();
            string bartId, homerId;
            using (var dataObjectStore = _dataObjectContext.CreateStore(storeName))
            {
                var context = new MyEntityContext(dataObjectStore);
                var bart = context.Persons.Create();
                bart.Name = "Bart Simpson";
                var homer = context.Persons.Create();
                homer.Name = "Homer Simpson";
                bart.Father = homer;
                context.SaveChanges();
                homerId = homer.Id;
                bartId = bart.Id;
            }

            // Test that the property is still there when we retrieve the object again
            using (var dataObjectStore = _dataObjectContext.OpenStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    var bart = context.Persons.FirstOrDefault(p => p.Id == bartId);
                    Assert.IsNotNull(bart, "Could not find Bart by ID");
                    var bartFather = bart.Father;
                    Assert.IsNotNull(bartFather, "Father property was not present on the returned person object");
                    Assert.AreEqual(homerId, bartFather.Id, "Incorrect Father property value on returned object");
                }
            }
            // See if we can use the property in a query
            using (var dataObjectStore = _dataObjectContext.OpenStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    var homersKids = context.Persons.Where(p => p.Father.Id == homerId).ToList();
                    Assert.AreEqual(1, homersKids.Count, "Could not find Bart with SPARQL query for Homer's kids");
                    Assert.AreEqual(bartId, homersKids.First().Id);
                }
            }
        }
示例#27
0
        public static void CreateDataTypeTestData()
        {
            var ctx = new MyEntityContext();

            var entity = ctx.DataTypeTestEntities.Create();
            var now    = DateTime.Now;

            entity.SomeDateTime         = now;
            entity.SomeDecimal          = 3.14m;
            entity.SomeDouble           = 3.14;
            entity.SomeFloat            = 3.14F;
            entity.SomeInt              = 3;
            entity.SomeNullableDateTime = null;
            entity.SomeNullableInt      = null;
            entity.SomeString           = "test entity";

            entity.SomeByte    = (Byte)255;
            entity.AnotherByte = (byte)128;
            //entity.NullableByte = null;
            //entity.AnotherNullableByte = null;

            entity.SomeSByte    = 127;
            entity.AnotherSByte = 64;

            entity.SomeBool = true;
            entity.SomeLong = 50L;

            entity.SomeSByte    = 127;
            entity.AnotherSByte = 64;

            entity.SomeShort    = 32767;
            entity.AnotherShort = -32768;

            entity.SomeUShort    = 65535;
            entity.AnotherUShort = 52;

            entity.SomeUInt    = 4294967295;
            entity.AnotherUInt = 12;

            entity.SomeULong    = 18446744073709551615;
            entity.AnotherULong = 52;

            //collections

            for (var i = 0; i < 10; i++)
            {
                var date = now.AddDays(i);
                entity.CollectionOfDateTimes.Add(date);
            }
            for (var i = 0; i < 10; i++)
            {
                var dec = i + .5m;
                entity.CollectionOfDecimals.Add(dec);
            }
            for (var i = 0; i < 10; i++)
            {
                var dbl = i + .5;
                entity.CollectionOfDoubles.Add(dbl);
            }
            for (var i = 0; i < 10; i++)
            {
                var flt = i + .5F;
                entity.CollectionOfFloats.Add(flt);
            }
            for (var i = 0; i < 10; i++)
            {
                entity.CollectionOfInts.Add(i);
            }
            entity.CollectionOfBools.Add(true);
            entity.CollectionOfBools.Add(false);
            for (var i = 0; i < 10; i++)
            {
                var l = i * 100;
                entity.CollectionOfLong.Add(l);
            }
            for (var i = 0; i < 10; i++)
            {
                var s = "word" + i;
                entity.CollectionOfStrings.Add(s);
            }

            ctx.SaveChanges();
        }
示例#28
0
        public void TestSetEntityCollection()
        {
            string storeName = Guid.NewGuid().ToString();
            string aliceId, bobId, carolId, daveId, edwinaId;
            using (var dataObjectStore = _dataObjectContext.CreateStore(storeName))
            {
                var context = new MyEntityContext(dataObjectStore);
                var alice = context.Persons.Create();
                var bob = context.Persons.Create();
                var carol = context.Persons.Create();
                alice.Friends = new List<IPerson> { bob, carol };
                context.SaveChanges();

                aliceId = alice.Id;
                bobId = bob.Id;
                carolId = carol.Id;
            }

            // See if we can access the collection on a loaded object
            using (var dataObjectStore = _dataObjectContext.OpenStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    var alice = context.Persons.FirstOrDefault(p => p.Id == aliceId);
                    Assert.IsNotNull(alice);
                    var friends = alice.Friends as IEntityCollection<IPerson>;
                    Assert.IsNotNull(friends);
                    Assert.IsFalse(friends.IsLoaded);
                    friends.Load();
                    Assert.IsTrue(friends.IsLoaded);
                    Assert.AreEqual(2, alice.Friends.Count);
                    Assert.IsTrue(alice.Friends.Any(p => p.Id.Equals(bobId)));
                    Assert.IsTrue(alice.Friends.Any(p => p.Id.Equals(carolId)));
                    var dave = context.Persons.Create();
                    var edwina = context.Persons.Create();
                    alice.Friends = new List<IPerson> { dave, edwina };
                    context.SaveChanges();
                    daveId = dave.Id;
                    edwinaId = edwina.Id;
                }
            }

            // See if we can access the collection on a loaded object
            using (var dataObjectStore = _dataObjectContext.OpenStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    var alice = context.Persons.FirstOrDefault(p => p.Id == aliceId);
                    Assert.IsNotNull(alice);
                    var friends = alice.Friends as IEntityCollection<IPerson>;
                    Assert.IsNotNull(friends);
                    Assert.IsFalse(friends.IsLoaded);
                    friends.Load();
                    Assert.IsTrue(friends.IsLoaded);
                    Assert.AreEqual(2, alice.Friends.Count);
                    Assert.IsTrue(alice.Friends.Any(p => p.Id.Equals(daveId)));
                    Assert.IsTrue(alice.Friends.Any(p => p.Id.Equals(edwinaId)));
                }
            }
        }
示例#29
0
 public void TestGuidAndNullableGuidDefaults()
 {
     var storeName = "TestGuidAndNullableGuidDefaults_" + DateTime.Now.Ticks;
     string testEntityId;
     using (var doStore = _dataObjectContext.CreateStore(storeName))
     {
         using (var context = new MyEntityContext(doStore))
         {
             var testEntity = context.Entities.Create();
             testEntityId = testEntity.Id;
             context.SaveChanges();
         }
     }
     using (var doStore = _dataObjectContext.OpenStore(storeName))
     {
         using (var context = new MyEntityContext(doStore))
         {
             var testEntity = context.Entities.FirstOrDefault(x => x.Id.Equals(testEntityId));
             Assert.IsNotNull(testEntity);
             Assert.IsNotNull(testEntity.SomeGuid);
             Assert.AreEqual(Guid.Empty, testEntity.SomeGuid);
             Assert.IsFalse(testEntity.SomeNullableGuid.HasValue);
         }
     }
 }
示例#30
0
        public void TestClearRelatedObjects()
        {
            var storeName = "TestClearRelatedObjects_" + DateTime.Now.Ticks;
            string aliceId;
            using (var dataObjectStore = _dataObjectContext.CreateStore(storeName))
            {
                var context = new MyEntityContext(dataObjectStore);
                var alice = context.Persons.Create();
                var bob = context.Persons.Create();
                var carol = context.Persons.Create();
                alice.Friends = new List<IPerson> { bob, carol };
                context.SaveChanges();
                aliceId = alice.Id;
            }

            using (var dataObjectStore = _dataObjectContext.OpenStore(storeName))
            {
                var context = new MyEntityContext(dataObjectStore);
                var alice = context.Persons.FirstOrDefault(p => p.Id.Equals(aliceId));
                Assert.IsNotNull(alice);
                Assert.AreEqual(2, alice.Friends.Count);
                alice.Friends.Clear();
                Assert.AreEqual(0, alice.Friends.Count);
                context.SaveChanges();
            }

            using (var dataObjectStore = _dataObjectContext.OpenStore(storeName))
            {
                var context = new MyEntityContext(dataObjectStore);
                var alice = context.Persons.FirstOrDefault(p => p.Id.Equals(aliceId));
                Assert.IsNotNull(alice);
                Assert.AreEqual(0, alice.Friends.Count);
            }
        }
示例#31
0
        public void TestSetAndGetSimpleProperty()
        {
            string storeName = Guid.NewGuid().ToString();
            string personId;
            using (var dataObjectStore = _dataObjectContext.CreateStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    var person = context.Persons.Create();
                    Assert.IsNotNull(person);
                    person.Name = "Kal";
                    context.SaveChanges();
                    personId = person.Id;
                }
            }

            // Test that the property is still there when we retrieve the object again
            using (var dataObjectStore = _dataObjectContext.OpenStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    var person = context.Persons.FirstOrDefault(p => p.Id == personId);
                    Assert.IsNotNull(person);
                    Assert.IsNotNull(person.Name, "person.Name was NULL when retrieved back from server");
                    Assert.AreEqual("Kal", person.Name, "Unexpected Name property value");
                }
            }

            // Test we can also use the simple property in a LINQ query
            using (var dataObjectStore = _dataObjectContext.OpenStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    var person = context.Persons.FirstOrDefault(p => p.Name == "Kal");
                    Assert.IsNotNull(person, "Could not find person by Name");
                    Assert.AreEqual(personId, person.Id, "Query for person by name returned an unexpected person entity");

                    // Test we can use ToList()
                    var people = context.Persons.Where(p => p.Name == "Kal").ToList();
                    Assert.IsNotNull(people);
                    Assert.AreEqual(1, people.Count);
                    Assert.AreEqual(personId, people[0].Id);
                }
            }
        }
示例#32
0
        public void TestManyToManyInverse()
        {
            string storeName = Guid.NewGuid().ToString();
            string aliceId, bobId, jsId, cssId, jqueryId, rdfId;
            using (var dataObjectStore = _dataObjectContext.CreateStore(storeName))
            {
                var context = new MyEntityContext(dataObjectStore);
                var alice = context.Persons.Create();
                alice.Name = "Alice";
                var bob = context.Persons.Create();
                bob.Name = "Bob";
                var js = context.Skills.Create();
                js.Name = "Javascript";
                var css = context.Skills.Create();
                css.Name = "CSS";
                var jquery = context.Skills.Create();
                jquery.Name = "JQuery";
                var rdf = context.Skills.Create();
                rdf.Name = "RDF";

                alice.Skills.Add(js);
                alice.Skills.Add(css);
                bob.Skills.Add(js);
                bob.Skills.Add(jquery);
                context.SaveChanges();

                aliceId = alice.Id;
                bobId = bob.Id;
                jsId = js.Id;
                cssId = css.Id;
                jqueryId = jquery.Id;
                rdfId = rdf.Id;
            }

            // See if we can access the inverse properties correctly
            using (var dataObjectStore = _dataObjectContext.OpenStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    var js = context.Skills.FirstOrDefault(x => x.Id.Equals(jsId));
                    Assert.IsNotNull(js);
                    Assert.AreEqual(2, js.SkilledPeople.Count);
                    Assert.IsTrue(js.SkilledPeople.Any(x => x.Id.Equals(aliceId)));
                    Assert.IsTrue(js.SkilledPeople.Any(x => x.Id.Equals(bobId)));
                    var css = context.Skills.FirstOrDefault(x => x.Id.Equals(cssId));
                    Assert.IsNotNull(css);
                    Assert.AreEqual(1, css.SkilledPeople.Count);
                    Assert.IsTrue(css.SkilledPeople.Any(x => x.Id.Equals(aliceId)));
                    var jquery = context.Skills.FirstOrDefault(x => x.Id.Equals(jqueryId));
                    Assert.IsNotNull(jquery);
                    Assert.AreEqual(1, jquery.SkilledPeople.Count);
                    Assert.IsTrue(jquery.SkilledPeople.Any(x => x.Id.Equals(bobId)));
                    var rdf = context.Skills.FirstOrDefault(x => x.Id.Equals(rdfId));
                    Assert.IsNotNull(rdf);
                    Assert.AreEqual(0, rdf.SkilledPeople.Count);

                    //  Test adding to an inverse property with some existing values and an inverse property with no existing values
                    var bob = context.Persons.FirstOrDefault(x => x.Id.Equals(bobId));
                    Assert.IsNotNull(bob);
                    var alice = context.Persons.FirstOrDefault(x => x.Id.Equals(aliceId));
                    Assert.IsNotNull(alice);
                    css.SkilledPeople.Add(bob);
                    Assert.AreEqual(2, css.SkilledPeople.Count);
                    Assert.IsTrue(css.SkilledPeople.Any(x => x.Id.Equals(bobId)));
                    Assert.AreEqual(3, bob.Skills.Count);
                    Assert.IsTrue(bob.Skills.Any(x => x.Id.Equals(cssId)));
                    rdf.SkilledPeople.Add(alice);
                    Assert.AreEqual(1, rdf.SkilledPeople.Count);
                    Assert.IsTrue(rdf.SkilledPeople.Any(x => x.Id.Equals(aliceId)));
                    Assert.AreEqual(3, alice.Skills.Count);
                    Assert.IsTrue(alice.Skills.Any(x => x.Id.Equals(rdfId)));
                }
            }
        }
示例#33
0
        public void TestOrderingOfResults()
        {
            string storeName = Guid.NewGuid().ToString();
            var peterDob = DateTime.Now.AddYears(-35);
            var anneDob = DateTime.Now.AddYears(-28);
            using (var dataObjectStore = _dataObjectContext.CreateStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    var joDob = DateTime.Now.AddYears(-34);
                    var mirandaDob = DateTime.Now.AddYears(-32);

                    var jo = context.Persons.Create();
                    Assert.IsNotNull(jo);
                    jo.Name = "Jo";
                    jo.DateOfBirth = joDob;
                    jo.Age = 34;
                    var peter = context.Persons.Create();
                    Assert.IsNotNull(peter);
                    peter.Name = "Peter";
                    peter.DateOfBirth = peterDob;
                    peter.Age = 35;
                    var miranda = context.Persons.Create();
                    Assert.IsNotNull(miranda);
                    miranda.Name = "Miranda";
                    miranda.DateOfBirth = mirandaDob;
                    miranda.Age = 32;
                    var anne = context.Persons.Create();
                    Assert.IsNotNull(anne);
                    anne.Name = "Anne";
                    anne.DateOfBirth = anneDob;
                    anne.Age = 28;

                    context.SaveChanges();
                }
            }

            using (var dataObjectStore = _dataObjectContext.OpenStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    var people = context.Persons.ToList();
                    Assert.AreEqual(4, people.Count, "Amount of people in context was not 4");

                    var orderedByName = context.Persons.OrderBy(p => p.Name).ToList();
                    var orderedByAge = context.Persons.OrderBy(p => p.Age).ToList();
                    var orderedByDob = context.Persons.OrderBy(p => p.DateOfBirth).ToList();

                    Assert.AreEqual("Anne", orderedByName[0].Name, "First in list was not alphabetically first");
                    Assert.AreEqual("Peter", orderedByName[3].Name, "Last in list was not alphabetically last");
                    Assert.AreEqual(28, orderedByAge[0].Age, "First in list was not numerically first");
                    Assert.AreEqual(35, orderedByAge[3].Age, "Last in list was not numerically last");
                    Assert.AreEqual(peterDob, orderedByDob[0].DateOfBirth, "First in list was not first by date");
                    Assert.AreEqual(anneDob, orderedByDob[3].DateOfBirth, "Last in list was not last by date");
                }
            }

        }
示例#34
0
        public void TestSetContextAndIdentityProperties()
        {
            string storeName = Guid.NewGuid().ToString();
            using (var dataObjectStore = _dataObjectContext.CreateStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    var person = new Person
                    {
                        Context = context,
                        Id = "http://example.org/people/123",
                        Name = "Kal",
                        DateOfBirth = new DateTime(1970, 12, 12)
                    };

                    context.SaveChanges();
                }
            }
            using (var dataObjectStore = _dataObjectContext.OpenStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    var found =
                        context.Persons.FirstOrDefault(p => p.Id.Equals("http://example.org/people/123"));
                    Assert.IsNotNull(found);
                }
            }
        }
示例#35
0
        public void TestSingleUriProperty(PersistenceType persistenceType)
        {
            var storeName = MakeStoreName("TestSingleUriProperty", persistenceType);
            using (var doStore = CreateStore(storeName, persistenceType))
            {
                string personId;
                using (var context = new MyEntityContext(doStore))
                {

                    var person = context.FoafPersons.Create();
                    person.Name = "Kal Ahmed";
                    person.Homepage = new Uri("http://www.techquila.com/");
                    context.SaveChanges();
                    personId = person.Id;
                }

                using (var context = new MyEntityContext(doStore))
                {
                    var retrieved = context.FoafPersons.FirstOrDefault(p => p.Id.Equals(personId));
                    Assert.IsNotNull(retrieved);
                    Assert.AreEqual("Kal Ahmed", retrieved.Name);
                    Assert.AreEqual(new Uri("http://www.techquila.com/"), retrieved.Homepage);
                }
            }
        }
示例#36
0
        public void TestBaseResourceAddress()
        {
            string storeName = Guid.NewGuid().ToString();
            using (var dataObjectStore = _dataObjectContext.CreateStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    var skill = new Skill { Id = "foo", Name = "Foo" };
                    context.Skills.Add(skill);
                    var otherSkill = new Skill { Id = "bar", Name = "Bar", Context = context };
                    var yetAnotherSkill = new Skill { Name = "Bletch" };
                    context.Skills.Add(yetAnotherSkill);
                    context.SaveChanges();

                    var found = context.Skills.FirstOrDefault(s => s.Id.Equals("foo"));
                    Assert.IsNotNull(found);
                    Assert.AreEqual("foo", found.Id);
                    Assert.AreEqual("Foo", found.Name);

                    found = context.Skills.FirstOrDefault(s => s.Id.Equals("bar"));
                    Assert.IsNotNull(found);
                    Assert.AreEqual("bar", found.Id);
                    Assert.AreEqual("Bar", found.Name);

                    found = context.Skills.FirstOrDefault(s => s.Name.Equals("Bletch"));
                    Assert.IsNotNull(found);
                    Guid foundId;
                    Assert.IsTrue(Guid.TryParse(found.Id, out foundId));
                }
            }
        }
示例#37
0
        public void TestGetAndSetDateTimeProperty()
        {
            string storeName = Guid.NewGuid().ToString();
            string personId;
            using (var dataObjectStore = _dataObjectContext.CreateStore(storeName))
            {
                var context = new MyEntityContext(dataObjectStore);
                var person = context.Persons.Create();
                Assert.IsNotNull(person);
                person.Name = "Kal";
                person.DateOfBirth = new DateTime(1970, 12, 12);
                context.SaveChanges();
                personId = person.Id;
            }

            // Test that the property is still there when we retrieve the object again
            using (var dataObjectStore = _dataObjectContext.OpenStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    var person = context.Persons.FirstOrDefault(p => p.Id == personId);
                    Assert.IsNotNull(person);
                    Assert.IsNotNull(person.Name, "person.Name was NULL when retrieved back from server");
                    Assert.AreEqual("Kal", person.Name, "Unexpected Name property value");
                    Assert.IsTrue(person.DateOfBirth.HasValue);
                    Assert.AreEqual(1970, person.DateOfBirth.Value.Year);
                    Assert.AreEqual(12, person.DateOfBirth.Value.Month);
                    Assert.AreEqual(12, person.DateOfBirth.Value.Day);
                }
            }

            // Test we can also use the simple property in a LINQ query
            using (var dataObjectStore = _dataObjectContext.OpenStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    var person = context.Persons.FirstOrDefault(p => p.DateOfBirth == new DateTime(1970, 12, 12));
                    Assert.IsNotNull(person, "Could not find person by Date of Birth");
                    Assert.AreEqual(personId, person.Id,
                                    "Query for person by date of birth returned an unexpected person entity");

                    // Test we can set a nullable property back to null
                    person.DateOfBirth = null;
                    context.SaveChanges();
                }
            }

            using (var dataObjectStore = _dataObjectContext.OpenStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    var person = context.Persons.FirstOrDefault(p => p.Name.Equals("Kal"));
                    Assert.IsNotNull(person);
                    Assert.IsNull(person.DateOfBirth);
                }
            }
        }
示例#38
0
        public void TestCustomTriplesQueryWithOrderedSubjects()
        {
            string storeName = Guid.NewGuid().ToString();
            var people = new Person[10];
            using (var dataObjectStore = _dataObjectContext.CreateStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    for (int i = 0; i < 10; i++)
                    {
                        var person = new Person { Age = 40 - i, Name = "Person #" + i };
                        context.Persons.Add(person);
                        people[i] = person;
                    }
                    context.SaveChanges();
                }
            }


            using (var dataObjectStore = _dataObjectContext.OpenStore(storeName))
            {
                using (var context = new MyEntityContext(dataObjectStore))
                {
                    var query = @"
select  ?s ?p ?o
where {
   ?s ?p ?o.
        ?s a <http://www.example.org/schema/Person>
}
order by ?s
";
                    IList<Person> results;
                    results = context.ExecuteQuery<Person>(new SparqlQueryContext(query) { ExpectTriplesWithOrderedSubjects = true }).ToList();
                    Assert.AreEqual(10, results.Count);

                    foreach (Person person in results)
                    {
                        Assert.AreNotEqual(0, person.Age);
                    }
                }
            }
        }
示例#39
0
        public void TestUriCollectionProperty(PersistenceType persistenceType)
        {
            var storeName = MakeStoreName("TestUriCollectionProperty", persistenceType);
            string personId;

            using (var doStore = CreateStore(storeName, persistenceType))
            {
                using (var context = new MyEntityContext(doStore))
                {

                    var person = context.Persons.Create();
                    person.Name = "Kal Ahmed";
                    person.Websites.Add(new Uri("http://www.techquila.com/"));
                    person.Websites.Add(new Uri("http://brightstardb.com/"));
                    person.Websites.Add(new Uri("http://www.networkedplanet.com/"));
                    context.SaveChanges();

                    personId = person.Id;
                }
            }

            using (var doStore = OpenStore(storeName))
            {
                using (var context = new MyEntityContext(doStore))
                {
                    var retrieved = context.Persons.FirstOrDefault(p => p.Id.Equals(personId));
                    Assert.IsNotNull(retrieved);
                    Assert.AreEqual("Kal Ahmed", retrieved.Name);
                    Assert.AreEqual(3, retrieved.Websites.Count);
                    Assert.IsTrue(
                        retrieved.Websites.Any(w => w.Equals(new Uri("http://www.techquila.com/"))));
                    retrieved.Websites.Remove(new Uri("http://www.techquila.com/"));
                    context.SaveChanges();
                }
            }

            using (var doStore = OpenStore(storeName))
            {
                using (var context = new MyEntityContext(doStore))
                {
                    var retrieved = context.Persons.FirstOrDefault(p => p.Id.Equals(personId));
                    Assert.IsNotNull(retrieved);
                    Assert.AreEqual("Kal Ahmed", retrieved.Name);
                    Assert.AreEqual(2, retrieved.Websites.Count);
                    Assert.IsFalse(
                        retrieved.Websites.Any(w => w.Equals(new Uri("http://www.techquila.com/"))));
                    Assert.IsTrue(retrieved.Websites.Contains(new Uri("http://brightstardb.com/")));
                }
            }
        }