Dani
Dani

Reputation: 15069

Insert new object with realtion to new objects with identity column

Customer class:

class Customer 
{ 
  int id; 
  int phone;
  int fax;
  PhoneNumber phone1;
  PhoneNumber fax1;
}

Class PhoneNumber
{
int id;
int prefixid;
string number;
}

The PhoneNumber was auto-Generated by EF4, I've change the auto-generated names to phone1 and fax1

Both phonenumber id and customer id are identity columns in the DB.

Now I want to create a new Customer:

       var newCustomer = Customer.CreateCustomer(0, CompanyID);
        PhoneNumber fax = new PhoneNumber();
        PhoneNumber phone = new PhoneNumber();

        fax.Customers.Add(newCustomer);
        phone.Customers.Add(newCustomer);

        context.Customers.AddObject(newCustomer);
        context.SaveChanges();

But now I get:

System.Data.UpdateException: {"Cannot insert explicit value for identity column in table 'PhoneNumber' when IDENTITY_INSERT is set to OFF."}

Why isn't EF4 deal with the identity column of the related table as it should deal with a new entity ? How can I make this work ?

(EF4 Should've create 2 entiries in Phones table, get their identity and add it to the customer row of the new customer - at least that what I want to do)

I know I Can create 2 Phone records, and than create the customer one, but

a. I want it to be done in one act.

b. if cusotmer creation fails - I don't want empty records on the phones table...

Upvotes: 0

Views: 2699

Answers (1)

RPM1984
RPM1984

Reputation: 73102

Well these two things contradict each other:

Both phonenumber id and customer id are identity columns in the DB.

But then the error:

System.Data.UpdateException: {"Cannot insert explicit value for identity column in table 'PhoneNumber' when IDENTITY_INSERT is set to OFF."}

It may seem like this error is saying a duplicate identity is being set, but it's not - it's saying your trying to explicitly set the identity field.

What that is saying is that the ID column in PhoneNumber is not an identity field. Or at least EF does not think it is.

So i have four questions:

1) Have you confirmed the field is set to IDENTITY in the EDMX? Maybe something has overriden it?

2) What does the CreateCustomer method do? Does it just new up a Customer and set some properties? It should not touch the identity fields.

3) Have you setup the navigational properties/cardinalities properly on the EDMX? E.g 1 Customer .. 1-* Phone Numbers.

4) Try adding the Phone number to the Customer, not the other way around. It makes sense to add the "many" to the "one" - e.g Customer is the aggregate here:

var newCustomer = Customer.CreateCustomer(0, CompanyID);
newCustomer.phone1 = new PhoneNumber();
newCustomer.fax1 = new PhoneNumber();
context.Customers.AddObject(newCustomer);
context.SaveChanges();

I'd also suggest redesigning your model.

It's silly to have Customer with 1 Fax and 1 Phone Number, each with a property on there. It should be a Customer have many "PhoneNumber" entities.

You could use TPH on the PhoneNumber entity to discriminate between Fax and other types, and the above code would make much more sense:

var newCustomer = Customer.CreateCustomer(0, CompanyID);
newCustomer.Phones.Add(new FaxNumber());
newCustomer.Phones.Add(new MobileNumber());
context.SaveChanges();

Upvotes: 1

Related Questions