C# Entity Framework Primary Key Violation - c#

I am getting data from an external API and want to save it in SQL Server with the Entity Framework. The data I am getting is order information. The data is roughly laid out like this:
Order:
Order ID
Customer
Shipping Address
Items
Customer:
Customer ID
Customer Details
The problem is that any customer can place more than one order. My entity framework is based around the Order information. So if a customer places more than one order, I get a primary key violation because the customer is already in the database even though the order is not.
For reference: I usually add orders like so: orderContext.Orders.Add(order);
Since every order contains the customer data, I can not add an order without "adding" a customer. Even if I were to check for an existing customer, I would still have to add the record to the Orders table, which would still trigger the problem. Is there any way to tell EF to add or update each "child" class?
The example here is more simplistic than the actual data. There are several other dependencies like this, such that even if I were to switch to using the Customer class as the basis for the EF, it would still have this problem.

Change your tables like this:
add CustomerID to your Order table, this is because everyone Order has just one Customer and every Customer has many or zero Order
Order(OrderID,CustomerID,ShippingAddress)
Customer(CustomerID,...)

Related

Entity Framework Code First with Dynamics Navision Tables

I have a project where I need to design a code first entity system with an existing database. These existing tables in the database are not just any tables, they were being created by Microsoft's ERP tool "Dynamics Navision". The Navision tool holds different company information and creates different tables per company. Let's assume there are 3 types of tables;
TableX
TableY
TableZ
And also there are 3 different companies;
CompanyA
CompanyB
CompanyC
The tool created 9 different tables from above combinations. It simply duplicates all of the tables and adds a prefix with the company name to the table names. At the end, our database looks like this;
CompanyA$TableX
CompanyA$TableY
CompanyA$TableZ
CompanyB$TableX
CompanyB$TableY
CompanyB$TableZ
CompanyC$TableX
CompanyC$TableY
CompanyC$TableZ
What Did I do so far?
So as you can see, there is an opportunity to simplify this architecture at the Entity Framework side. To achieve this, I created 3 entity classes for TableA, TableB, TableC and at the run time, I let the user to choose a company and according to the chosen company, I reflected my entity class with a custom TableAttribute where the table name prefixed with the company name. I am not going to give details on how I achieved it right now(but you can find implementation details on this article: LINK) but so far so good and I have applied all of the previous steps successfully and I managed to be dynamic on table names with the chosen company. However, there is another problem.
What is my question?
The problem is, even though I managed to create a system where I can change the entity class' table name attribute at the runtime and access to the target companies tables, there were no guarantee that duplicated tables with different company name prefixes are sharing %100 same inner field architecture. For example, CompanyA$TableX can have 5 fields while CompanyB$TableX has 6 fields where the first 5 fields are the same with CompanyA$TableX's fields but the last 1 field is extra. Let's also visualize it;
CompanyA$TableX's table fields;
ID
Name
Surname
Adress
PhoneNumber
CompanyB$TableX's table fields;
ID
Name
Surname
Adress
PhoneNumber
EMail
As you see, the Email field is the extra however the table names are the same(TableX), only the company differs and in my system, they share the same entity class and the company name determined at the run time as I mentioned before.
So, I want to know about how can I achieve to be dynamic on this. How I can have only one entity class but be dynamic in the fields of this class according to the chosen company. I want to know if it is possible technically and if it is, how to implement it. Thanks in advance.
What you are saying about Nav is not true. It is not possible to create the same table in Nav that will have different structure per company. Even the modern extension architecture will not give you that kind of result.

How can I explore EF relationships and models at runtime?

I have a model call "Customer" which is to be linked to several other models (tables) in Entity Framework. When deleting the said Customer, I need to show all the related records and their counts and finally list all the records before allowing the user to delete the customer.
For example I might say this Customer is related to : Orders (1), Order_Items (5) where a customer is directly linked to an Order, and then an Order is linked to Order Items, but deleting the Customer will impact records in both the tables.
Is there a way to do this in .net dynamically or will I need to have a static view where I update all this info every time I make changes to the model and relationships?

How to make composition where both entities have 'has' a relation with each other

Taking a simple example:
Customer can place order on ecommerce website.
Now, I can infer two composition ('has a' relationship) from this statement.
Order has a customer.
Customer has one or multiple orders.
How should i create class for this. What all factors should i keep in mind while designing these two classes. Below are possibilities i have found till now.
Order class with customerId.
Order class with Customer object.
Customer class with no order history. (Since i can still find information about customer order from order table)
Customer class with List of OrderId.
Customer class with List of Order objects.
How do i decide which is best for a situation?
The first possibility is the simple one which seems enough for your case! An order will have the customer id as a foreign key.

Creating a custom entity with the Entity framework

If I have for example two entities, lets say Customers and Staff with no relation between them, is it possible to create a third entity which doesn't have a corresponding table in the database which takes some information from the first and the second entity and also one or two additional columns (for example computed columns)?
You can join entities through this: http://blogs.msdn.com/b/simonince/archive/2009/03/23/mapping-two-tables-to-one-entity-in-the-entity-framework.aspx
But I do believe they have to be related; otherwise, how would it know what information was correctly tied to each other? The only thing it could do is query all rows of one entity and match them up with all rows of the other, which is probably not what's desired.

How to access foreign key related column values using LinqToSql?

I'm trying to wire-up LinqToSql to be my data access layer and running into a few issues, probably due to my lack of experience with LinqToSql.
I have two tables, one called Project and one called Employee. The Project has fields for OpenedBy and ClosedBy which are foreign key references to the Employee table, which has fields for EmployeeId and Name.
When I fetch a Project I would like for it to fetch the EmployeeName for the OpenedBy and ClosedBy. I would like to access these like the following:
// assuming data is of type project
this.OpenedByName.Text = data.OpenedByName;
this.ClosedByName.Text = data.ClosedByName;
Is it also possible to set these values whenever OpenedBy or ClosedBy changes? Is this possible? Sample code would be much appreciated!
Clarification
I would like to do this without having to use stored procedures.
If you have 2 relationships coming from the Employee table, I think you'll have 2 child properties, project.Employee, and project.Employee1 in each Project entity.
You can change the name of the association, just go to the relationship properties, select Child Property and there change the name of each child Employee to be more descriptive.
You can name the child properties as you want, for example you could:
this.OpenedByName.Text = data.OpenedByEmployee.Name;
this.ClosedByName.Text = data.ClosedByEmployee.Name;

Categories

Resources