0

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);
        }
    }
Renier
  • 480
  • 5
  • 13
  • CORRECTION: TYPO "now new row..." should read "no new row...", i.e. nothing happens on the db side – Renier Jan 23 '12 at 13:24

1 Answers1

0

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(); 
Pankaj Upadhyay
  • 12,966
  • 24
  • 73
  • 104
  • Thanks for reply, but see correction above. There was a typo. – Renier Jan 23 '12 at 17:43
  • What's `AddToRelationshipTypes(type)` ? can you show it's code. BTW, why don't you simply use `c.RelationshipType.Add(type);` and then call `c.SaveChanges();` – Pankaj Upadhyay Jan 23 '12 at 17:52
  • AddToRelationshipTypes(type) is a generated method. public void AddToRelationshipTypes(RelationshipType relationshipType) { base.AddObject("RelationshipTypes", relationshipType); } Interesting is that I do not have a c.RelationshipTypes.Add method, only a .AddObject method whichnis effectively the same as the above. – Renier Jan 24 '12 at 01:24
  • **SOLVED**: I guess I am just stupid or have not done enough research. When building VS actually copies the SQL CE file into the bin (Debug/Release) directory. In my case the one for the test project. I was looking at he wrong DB all along!!!!! Hope this saves someone else from wasting a whole day. – Renier Jan 24 '12 at 02:44