1

I seem to have trouble adding objects to tables that have a 'n to n' relationship.

Tables are defined as follows:

Table A

  • ID (PRIMARY)
  • ...
  • ...
  • ...

Table B

  • ID (PRIMARY)
  • ...
  • ...
  • ...

Table C

  • TableA_ID (index)
  • TableB_ID (index)

So basically Table C links Table A and B, by their IDs. Using the entity framework we now have an object TableA containing an Entity Collection of TableB entities.

However when I add an existing object of type TableB to the TableA.TableBs entity collection property, I receive an exception:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(SELECT\n TableC.TableA, \n ' at line 1

It seems that I'm trying to do a very normal / common thing, however I've not been successful getting this to work.

C# code:

var database = new DatabaseEntities();
var tableAObject = database.SingleOrDefault(e => e.ID == 1);
var tableBObject = database.SingleOrDefault(e => e.ID == 1);

tableA.TableBEntities.Add(tableBObject);
database.SaveChanges();

Apparently I'm doing something wrong, so my question is, how should I add an object to Table C?

4

0 回答 0