I am using Effort in Visual Studio and C# to recreate and unit test a database. I am writing a class for a single table and so I am only populating the database with objects from that table. My problem is that the Effort database wants the object's foreign keys to reference actual objects in the database.
My error is
System.Data.Entity.Infrastructure.DbUpdateException: An error occurred while updating the entries. See the inner exception for details.
System.Data.Entity.Core.UpdateException: An error occurred while updating the entries. See the inner exception for details.
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
NMemory.Exceptions.ForeignKeyViolationException: Foreign key violation [Table1 :: SettingsId]. The key value  does not exists in the referenced table [Table2:: SettingsId].. Error code: RelationError
As this particular table has many foreign keys to objects that have many other foreign keys it would require a lot of work. Is there any way in Effort to turn this off so that I can test this table alone?
I ran into this problem too and wanted to test the table in the scope of the larger database. You can make a helper method that will just instantiate everything you need and pass the database or table with references as a result. That helps because you can call it from whichever test method/class you need to and whenever you need to.
You could create a new .edmx file from your database for testing use only. For example:
In Visual Studio select File->New->Data->Entity DataModel. Select "EF Designer from database". Choose or create a connection to your existing SQL Server database and save the connection settings in app.config as "testConnectionString".
In the data model designer, remove any foreign key relationships you don't need. Save the designer and rebuild the project.
In Effort, pass the name of the connection string (that was generated when you created the new .edmx) to the
CreateTransient method, like this:
EntityConnection connection = Effort.EntityConnectionFactory.CreateTransient("name=testConnectionString"); MyDbContext context = new MyDbContext(connection);
This should allow you to insert values into the table without needing to populate the related foreign key tables.
This comes with a warning, though - if the code you are testing uses any navigation properties that depend on the foreign keys, it will fail. This makes me wonder how valuable this sort of "unit testing" really is.