views:

35

answers:

1

My setup is this; first I'm defining a couple of new rows.

newCompany = new company
             {
                companyInfo = value.company_info,
                companyName = value.company_name,
                organizationNumber = value.company_orgnr
             };
newContract = new contract
              {
                 contractName = value.contract_name
              };

Then I define a new row in the contractToCompany table:

contracttocompany newContractToCompany = new contracttocompany { company_idCompany = newCompany.idCompany, contract_idContract = newContract.idContract };

And then I save changes

context.SaveChanges();

I have autoincrementing id-columns on all tables. Is this the right order to do things or will newContract.id and newCompany.id == default or null?

+1  A: 

Your model is not built in the way EF v1 expects. If you want to do it this way you first have to add company and contact and save changes. This operation will reload Ids of these entities and you can than define contactToCompany and save changes again.

EF v1 expects that you model M:N relation in the way of navigation properties. You can expose M:N relation as two 1:N relations to separate link entity but you have to do it manually. Still you will have to use navigation properties instead of foreign keys. How did you create your mapping?

Ladislav Mrnka
Thanks for your insightful answer, I think I know how to tackle the problem now. I mapped by creating an entity and then generating from existing DB. The relations are preserved (or so it seems) in the model so I thought all was fine in doing it that way. For EF v1 to understand the model all relations need to be M:N? How annoying.
Phil