7

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 [0] 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?

3 Answers 3

1
+50

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.

0

The table is not alone. If you want the table alone, design a new table and drop the constraints and references using .sql scripts. When using Entity Framework it is relatively easy to mock the inserts but that can get pretty intense.

0

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.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.