Search code examples
entity-frameworksql-server-ce-3.5

Unable to persist Entities to SQL CE using entity framework


I am stating out with entity framework. I have created my ADO.NET Entity Model and mapped the entities to a local SQL CE database file (all done via the wizards). I have created a unit test to test the data access and see how things work. The test executes fine and without any exceptions. However, no new row is generated in the database. Please Help!!!

    public void TestCreateRelationshipType()
    {

        using (var c = new TenderModelEntities())
        {
            IList<RelationshipType> types = c.RelationshipTypes.ToList<RelationshipType>();
            int num1 = types.Count();
            RelationshipType type = new RelationshipType();
            type.Description = "New Client";
            c.AddToRelationshipTypes(type);
            c.SaveChanges();
            IList<RelationshipType> types2 = c.RelationshipTypes.ToList<RelationshipType>();
            int num2 = types2.Count();
            Assert.AreEqual(num1 + 1, num2);
        }
    }

Solution

  • New row is added to the database because you call the SaveChanges() function. When you call this on your datacontext, the changes are passed on to the database.

    If you don't want to make any changes to the database, just comment out this section like below

    // c.SaveChanges();