For example in our case Code First attempted to turn on cascade delete for both BillingAddressId and DeliveryAddressId columns in the Users table. What's a Multiple Cascade Path Anyway?A Multiple Cascade Path happens when a cascade path goes from column col1 in table A to table B and also from column col2 in table A to table B. See previous errors.Īs you can tell from the type of the inner exception (SqlException), it has nothing to do with EF or Code First it has been generated purely by SQL Server when Code First was trying to create a database based on our object model. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Introducing FOREIGN KEY constraint 'User_DeliveryAddress' on table 'Users' may cause cycles or multiple cascade paths. That said, we won't use this data annotation and will go with the fluent API way for a reason that you'll soon see.Ĭreating a SQL Server SchemaThe object model seems to be ready to give us the desired SQL schema, however, if we try to create a SQL Server database from it, we will get an InvalidOperationException with this message: The Object ModelLet's start by creating an object model for our domain: But in any case, two users can’t share the same address for the same purpose. With several foreign key columns (which is the case in our domain model since we also have a foreign key for DeliveryAddress), we can reference the same address target row several times. This isn’t as strong as the guarantee from a shared primary key association, which allows a particular address to be referenced by at most one user, period. For example, by making the BillingAddressId column unique, we declare that a particular address can be referenced by at most one user, as a billing address. An additional constraint enforces this relationship as a real one to one. One table has a foreign key column that references the primary key of the associated table (The source and target of this foreign key constraint can even be the same table: This is called a self-referencing relationship.). One-to-One Foreign Key AssociationInstead of sharing a primary key, two rows can have a foreign key relationship. The following class diagram demonstrates the domain model: Introducing the Revised ModelIn this revised version, each User always have two addresses: one billing address and another one for delivery. Today we are going to discuss how this is done by learning about one-to-one foreign key associations. In the third part of this series we saw the limitations of shared primary key association and argued that this type of association is relatively rare and in many schemas, a one-to-one association is represented with a foreign key field and a unique constraint.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |