One to One Error in Entity Framework 4

I already read One-to-One Card Issues for Entity Framework , and this does not duplicate, as business rule rules are specified here.

There are two tables: Accounts and Orders.

Invoices
-> InvoiceID (Primary, Auto Number)
Orders
-> OrderID (Primary, Auto Number)
-> InvoiceID (FK InvoiceID of Invoices Table)

Now the problem is that for this association, EF requires a one to many relationship if the property names do not match. If the property names are the same, then it serves the purpose of the derived class, but here Order is not a derived class or Invoice.

InvoiceIDs are generated for each shopping basket, but OrderIDs are generated only for paid invoices, so each order has an InvoiceID, but each order does not have a corresponding invoice.

If I create a separate table for this, then I need to write too much code for this. Is there a way to remove this restriction and let EF handle my model.

However, at present, if I change the model as follows, it works

Invoices
-> InvoiceID (Primary, Auto Number)
Orders
-> OrderID (Auto Number)
-> InvoiceID (Primary, FK InvoiceID of Invoices Table)

But is this a good practice? Because, by definition, the InvoiceID of the Orders table will certainly be unique, but we will refer everywhere to OrderID for comparison and to many other links. I know that I can index a property, but I don’t feel that this design is perfect.

One to One Error

+3
source share
1 answer

, , 1: * - EDM 1:1. , , , , .

- . , EF3.5, .

InvoiceID Order .

, :

  • InvoiceID Order.
  • Asscoation Invoice Order.
  • "" " " .
  • , "".
  • " " .
  • " " , .
  • "". .
  • "" .
  • , "End2 Multiplicity", * Collection of Orders, 1 (One of Order) .
  • , Validate. , , , .

, , : (, Order.InvoiceID) 1:1 (-) (), (InvoiceID).

, EF4.0 Lazy Loading - , . , (-) -, , :

Order order = context.Orders.First();
int invoiceID = order.Invoice.InvoiceID;

, "", "Lazy Load" "Eager Load Load":

int invoiceID = order.InvoiceReference.EntityKey.EntityKeyValues[0].Value;
+2

Source: https://habr.com/ru/post/1774088/


All Articles