views:

48

answers:

3

I have two tables Plants and Information. For every plant there are many information, but for each plant there is a single MainInformation. So there is a one-to-many relationship and a one-to-one relationship between the two. The Information table has a PlantID and the Plants table has a MainInformationID. I want both fields in both tables not to be nulls. But now you can't insert either of the two records into their tables because each one requires their fields not be null, meaning they need the other record to be created first in order to create themselves. Perhaps this is not a good database design and something should be changed? (I am new to databases and entity framework)

I tried inserting into the database itself manually but I cant do it. I also tried this code with EntityFramework.

using (var context = new MyEntities())
        {
            var p = new Plant()
            {
                LatinName = "latinNameTest",
                LocalName = "localNameTest",
                CycleTime = 500
            };

            var i = new Information()
            {
                ShortDescription = "ShortDesc",
                LongDescription = "LongDesc"
            };

            p.MainInformation = i;
            i.Plant = p;

            context.AddToPlants(p);
            context.AddToInformation(i);

            context.SaveChanges();                
        }
+2  A: 

You need to change the tables to allow for null. There is no other way to do this.

Hogan
One of the two tables anyway. It might be a good idea to change requirements so that information can only be added to a plant, but the plant can only assign the main information to one of it's existing information records.
NickLarsen
@downvoter : feel free to explain.
Hogan
I'm not the down voter, but I think your answer is factually incorrect. @gbn gives some other choices, a fourth would be to split the relationship into a separate table.
Craig Stuntz
@Craig : Well his first option is the one I gave -- it is clearly the best option. 2nd choice ruins the data integrity rules in the DB; an option but a poor one, and it is the clear the OP wants data integrity. The 3rd option is basically the same as allowing NULL except it does not follow the expected standards of SQL -- basically it allows you to "rename" null, making the design of the DB non-standard (this is why null exists). Your option is good, I'd use it, it is an advanced choice, not always needed, we would need to find out more about the schema; this answer is clearer for beginner.
Hogan
A: 

You may want to look at database transactions and how to use them with the Entity Framework. You can wrap both INSERTS into a single db transaction so the only results are both of them go in or neither go in.

Here is a link for transactions using EF. I didn't read through it but it seems to talk about them enough to get you started.

Cody C
This won't work because the FKs are checked for each statement in the transaction
gbn
+2  A: 

One of

  • The 1-1 FK column has to be NULL
  • The FK has to be disabled to allow parent insert before child
  • You have a single dummy Information row that is used by default in FL column

SQL Server does not allow deferred constraint checking without "code change" rights so even wrapping in a transaction won't work

Sounds like an EAV schema, which has other problems

gbn
I'm gonna go with the first option, I also considered the third option but we didn't go with it. I'm interested in the second option you mentioned. Could explain a bit more on how to do it?
sklitzz
ALTER TABLE MyTable NOCHECK CONSTRAINT FK... http://msdn.microsoft.com/en-us/library/ms190273(SQL.90).aspx
gbn
Also, I answered here: http://stackoverflow.com/questions/737115/turn-off-constraints-temporarily/737167#737167
gbn