Save data in child table with parent table having composite keys - c#

I wonder why it is not working.
I am using ASP.NET MVC having dbcontext to contact with database.
I have two tables.
Table A and Table B
Table A has multiple columns from that there are three columns i.e. Email, Year and Month which are primary keys / composite keys.
Table B has a 5 columns from which one column is the TableAid that is use to store the auto incremented first column from the TableA without relationship as a foreign key, just store the id.
I pass the details from the view to controller as a one object i.e. TableA class and TableA has a list collection of TableB having multiple values.
Now I store the first TableA data first and than by taking id of the TableA and store TableB's data one by one.
Here in above process I got a exception while storing the details in TableB.
i.e.
Invalid column name 'Email'.
Invalid column name 'Year'.
Invalid column name 'Month'.
Which are composite keys.
I am not able to store child tables details.
Can anyone having any solutions for this?
public class TableA
{
//all fields get set
virtual public ICollection<TableB> TableB { get; set; }
}

Related

Can someone explain what a Foreign Key is, and why you use it? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm in the middle of learning C# and i'm going towards learning about SQL databases, but i'm not sure i quite understand the Foreign-Key?
Thank you beforehand
A foreign key is another table's primary key. It is used to link the two tables.
Example: we have 2 tables (Person & Employee)
Person has primary key: personID,
Employee has primary key: employeeID
An employee is a person so they have to be linked.
Employee has foreign key: personID
So, for every Employee there is a Person linked to it (where the personID in Employee is the same as the personID in Person).
In the context of relational databases, a foreign key is a field (or collection of fields) in one table that uniquely identifies a row of another table or the same table. In simpler words, the foreign key is defined in a second table, but it refers to the primary key or a unique key in the first table.
This takes us to Primary key. The customers table contains a unique key on each row called customerNumber this is the primary key for the table. On the orders table we have the orderNumber column which is the primary key for that table.
The orders table has a foreign key link back to the customers table though the customer Number. We call the customer Number the foreign key.
Customer Table:
customerNumber CustomerName.
1 Bob
2 Jane
Order table:
OrderNumber customerNumber Status
1 1 Shipped
2 1 Exploded
Using the data above if you wanted to see what orders bob had you would take the primary key being bobs customer id and check the order table for all rows containing the it. This is the foreign key linking two tables.
A FOREIGN KEY is a key used to link two tables together.
A FOREIGN KEY is a field (or collection of fields) in one table that refers to the PRIMARY KEY in another table.
The table containing the foreign key is called the child table, and the table containing the candidate key is called the referenced or parent table.
Reference : https://www.w3schools.com/sql/sql_foreignkey.asp
You use it to link two table together and it makes your data integrity better. You should always use foreign keys to link table references, unless you have clear reason not to.
Let's say you have Foobar table with UserId column and User table with Id column. You use foreign key to link UserId column from Foobar table to User table's Id column.
After that if you have Foobar row with UserId of 1 and try to delete row from User that has Id column value of 1 you get an error because Foobar references it. Also inserts are validated, let's say you try to insert row to Foobar table with UserId that doesn't exists in User table and you'll get an error as well.

Disable associations when a foreign key do not exist

I've created an edmx for my database. In it Entity framework removed a table and instead created an association between two tables because it matches a column name with the primary key in the other table.
I do not want that as there is no real association between those tables. How can I remove that association and get a class for the middle table instead?
Example:
SomeTable
Id int pk
MiddleTable
SomeTableId int fk
SomeCode int
OtherTable
SomeCode int pk
It's the MiddleTable which do not get a class.
Remove one table from the edmx, e.g. OtherTable.
Update model from database and add MiddleTable.
Update model from database and add OtherTable.
When I do this with a similar model I end up with an association between SomeTable and MiddleTable and an unassociated OtherTable. Now you can add/remove associations manually as you wish.
It's normal EF behavior not to create a class for the middle table. This is a so-called many to many association between SomeTable and OtherTable which can be modelled by two collection properties:
SomeTable.OtherTables
OtherTable.SomeTables
The middle table, the junction table, is not really necessary.
It's a bit surprising to me that you say that there is no association between the two tables although, apparently, in the database there are foreign keys. Technically, it is a many to many association.

Creating a relationship between two tables with a Composite Primary Key in Entity Framework 4

I've a database in SQLServer 2008.
I've a table "Table1" with three rows as the primary key and I need to create a relationship with another table "Table2" but I don't want to add the three fields of Table1 in Table2 to be able to create the relationship.
So I've added a new field in Table1 named IdRow of type Int and Indentity=yes and I've created an index of type "Unique Key" for this new field so I can create the relationship based on this new field instead the three others.
So far so good but when I import the two tables into the model in the Entity Framework's model it doesn't recognize the IdRow as unique so I can't create the relationship with Table2 based on this field.
Can someone help me with this?
Is there a workaround to solve this or I've to add the three fields of Table1 in Table2 to create the relationship?
Well, I would do the opposite.
Can't you put the new idRow as the primary key, and add a UNIQUE Constraint on your 3 other fields ?

Manual Entity Framework association

I have the following tables:
Table Group (
id INT PK,
year INT PK,
name VARCHAR
)
Table Person (
id PK,
GroupID INT,
name VARCHAR
)
The database does not have foreign keys defined so I want to create a manual association from the Person tables GroupID to the Group tables id.
To do this I right click Person and Add an association. I create a Many to One association and everything works. The problem is when I go to add the mapping. Because the Group table has two primary keys entity framework was something from the Person table to map to the year key.
What do I need to do to create the association?
You cannot create such association because EF follows same rules as database. All PK columns from principal entity must exists as FK columns in dependent entity.
The only way can be some database view selecting distinct Groups with Id and Name and mapped as read only entity and build navigation between those two. I didn't try it but I guess it should work. It will have its own disadvantages because you will have two completely unrelated entities for group and the entity related to person will not accept any modification (without mapping custom SQL commands or stored procedures to insert, updated and delete operations).

How can add TablexId and TableyId to a relationships table ( shows up as navigation property on Entity Framework )?

I have a Tags table and an Objects table.
A record of their relationships is kept via the ObjectTags table which has two columns. The columns store ObjectId and TagId (from the Tags and Objects tables), and both make up a composite key (can't have TagId and ObjectId twice).
In Entity Framework this table is not mapped as an object but rather enable "navigation" between the main tables. This is very cool, but how to i add to this table? What is the best way?
I add an Object and now i have it's ObjectId. I also add new Tags (reuse the already existing ones) and get their TagId. Now i should add the ObjectId and TagId to this relationship table... but how?
I had to use something like this:
db.Items.Where(id => id.Id == newItem.Id).First().Tags.Add(newTag);
This means. In the "db" in the "Items" table where "the Item Id is the one i'm looking for", select it, then go via the navigation property to the "Tags" table and add a "new Tag".
Doing this updates the relationship's ItemTags table with the new ids.
:)

Categories