views:

268

answers:

3

I have objects: type A (Id, Name), type B (AId, Description).

I want to make relation 1-to-1 (and create it 1-to-[0..1]). All works great exept deleting objects of type A. When I'm trying to delete some object from type A exception occurs.

A relationship is being added or deleted from an AssociationSet ‘...’. With cardinality constraints, a corresponding ‘...’ must also be added or deleted.

Im searched for solution (found editing CSDL for many-to-many), but nothing helps. There is also cascade action defined in the table in Database.

Any suggestions?

UPD: Thanks for answers. Let's say more clear.

  1. I don't want to implement inheretance betwen A and B

  2. I try to fix problem by editing edmx file (like this http://codepolice.net/2008/12/16/cascade-delete-in-entity-framework/), but no luck. Seems it's only worked for one-to-many.

  3. I just want to have 2 objects with one-to-one relation. For example, Order and OrderDetails. I expected automatic creating/deleting OrderDetails for every Order I have.

A: 

One way to do this is to have a single entity that maps to 2 tables. See:

http://msdn.microsoft.com/en-us/library/bb896233.aspx

Shiraz Bhaiji
I know about it, and partially solve the problem by this way. But I don't want a single entity :)
lak-b
One question about "One entity - two tables". How can I make automatic creation in second table when new record in first created?
lak-b
There is not an automatic way of doing this
Shiraz Bhaiji
A: 

Visual EntityFramework tool doesn't recognize correctly 'on delete cascade' and creates incomplete xml mapping. You have to change edmx file (You can do it with notepad). Instructions here:

http://codepolice.net/2008/12/16/cascade-delete-in-entity-framework/

Worked for me.

LukLed
It's about one-to-many and it's seems to be worked. But it's not worked for one-to-one.
lak-b
You can just delete row from B first. It seems that there is no easy way, it just needs to be corrected in framework.
LukLed
> You can just delete row from B firstNo, I cant :( Error "A relationship is being added or deleted from an AssociationSet..." occurs.
lak-b
A: 

1:1 should give an exception if your deleting B right?

I think what you want is 0..1

Right click Add->associations.

under multiplicity: On the left hand side choose One for A and 0 or 1 on the right for B. I think you need this if you want an optional description object(B) for A.

You could also move B's fields into A and check not null for those fields right? That might be easier, then I think you could just use A's fields.

Also, I'm not a database designer by a long shot but, wouldn't you want the Data of A in A?

If for instance you had "Person" and his "Home", I would think those would be a good case for 1:1 (or 0..1 real world), because they themselves are 2 distinct objects that other objects can share independently.

Seems like the A_DataObjects just leads to an unnecessary join?

Old Answer below (not looking for inheritance, but leaving for someone else): OK, I think I ran into this today. I think what you might want to do is define 2 classes as subclasses of a base class (entity). Right click and do add -> inheritance to get started. I didn't get this all working yet, but I think it involves specifying a field in the base, BaseType which can be used to key in on the derived classes.

http://mosesofegypt.net/post/Inheritance-and-Associations-with-Entity-Framework-Part-1.aspx

Note, there's a part 2 and 3 of this.

-David

David
I don't want inheritance :) I just want to have, for example, Order and OrderData objects.
lak-b
Ok, I left some more thoughts below.
David