New column issue after association - c#

I created 2 tables in Visual studio say TA and TB. In edmx file I made 1-to-many association between TA and TB. After making association, a new column is generated in TB named TAId.
I just want to know whether I have to add that column in the TB table in database?
EDIT : Exception I got:
nCeremony.msl(23,10) : error 3004: Problem in mapping fragments starting at
line 23: No mapping specified for properties CourseOption.MenuId in Set
CourseOptions.
An Entity with Key (PK) will not round-trip when:
Entity is type [CeremonyDBModel.CourseOption]
Ceremony.msl(31,10) :
error 3004: Problem in mapping fragments starting at line 31:No mapping
specified for properties Menu.CeremonyId in Set Menus.
An Entity with Key (PK)
will not round-trip when:
Entity is type [CeremonyDBModel.Menu]

A one-to-many association is done by having records in a child table reference their parent.
Consider an example of customers and purchases. A customer may make many purchases, but each purchase can only belong to one customer. Consider the following two tables:
customers purchases
============ ===========
id name id customer_id product
Each customer has a unique ID and a name. Each purchase also has a unique ID and a product name. However, in order to have a one-to-many association, the purchases table must also reference which customer made the purchase. You couldn't do this in reverse, or you would have duplicate names of customers each referencing some purchase.
For example:
customers purchases
============ ===========
id name id customer_id product
--- -------- --- ------------ --------
1 John 1 1 apple
2 Mary 2 1 orange
3 2 banana
In this example you can see that John purchased an apple and a orange, and Mary purchased a Banana.
The customer_id column is necessary for the purchases table to establish the relationship with the customers table.
By creating the association between your TA and TB tables, Visual Studio has created the column TAid for you, because that is how relational databases like MS SQL Server handle them.

Related

EF6 Updating 3 tables in one go

I have a database with 3 tables
Table
Customer
CustomerID (PK)
Name
Table
Order
OrderID (PK)
CustomerID (FK)
OrderDate
Table
OrderDetailLine
OrderDetailLineID
OrderID (FK)
Price
ProductCode
What I would like to do in the Entity Framework is add 1 customer, then add several different Orders, each with many OrderDetailLine all relating to the first customer inserted. I have hooked up all the relationship in SQL Server and imported them into EF model. All no problems. I can insert 1 customer and many orders and OrderDetailLine first time all records get inserted ok into the correct databse tables
I am looping around customer and orders in a file adding then adding the customer and orders to the database
But when I want to add another order for the same customer( I am getting a Primary key violation on Customer with CustomerID). EF is trying to re-insert the same customer after my initial context.SaveChanges();
So, how do I stop EF from trying to add the same customer when using the same datacontext?
I have been going around in circles for hours and getting the same error
{"The INSERT statement conflicted with the FOREIGN KEY constraint \"FK_Order_Customer\". The conflict occurred in database \"xxxxx\", table \"dbo.Customer\", column 'CustomerID'
The customer is newly created along with the order and orderlines. But it works first time and I get the CustomerID back from EF. But when I add new order and orderlines after the inital context.savechanges(); I get the FK error
Thanks for taking the time.
The problem was I was just calling DataContext.SaveChanges() with adding the entities to the correct model. For example:
Customer.Order.Add(customerInfo);
Well it was my lack of understanding.

Does Entity Designer support many-to-many relationships with duplicate association data?

Package A has one Desktop Y and one Monitor Z. Package B has one Desktop Y and two Monitor Z's. I want to represent that relationship in a database created by Entity Designer.
There's a many-to-many relationship between StandardPackage and StandardMachine (which has desktops, monitors, laptops, etc). Every quarter, we update our packages, and clients place orders. They select a package, and when the order is placed, that package's StandardMachines dump their data (type, make, model, etc.) into newly created Machines.
The problem lies in the way Entity Designer creates the junction table for the StandardPackageStandardMachine relationship. The junction table has two columns corresponding to the IDs of the records on each end of the relationship, and the primary key for the junction table is the combination of those two IDs. So, for the example in the first paragraph, if Package A's Id is 1, Package B's Id is 2, Desktop Y's Id is 1, and Monitor Z's Id is 2, what I need is
StandardPackageId | StandardMachineId
------------------|------------------
1 | 1
1 | 2
2 | 1
2 | 2
2 | 2
but obviously you can't have two 2/2 records.
Is there a nice way to tell Entity Designer what I intend to do, so that it will create an auto-incrementing primary key column for the junction table? Or do I have to resort to manually creating the junction table and handling the association myself?
By the way, if someone even knows how to rephrase my question title, please do so.
Or do I have to resort to manually creating the junction table and handling the association myself
Yes, you have. Because the table is not a junction table, but an entity.
The semantics of a many-to-many association are subtly different from what you want to express. One record in a regular n:m association table expresses "A is related to B". In your table it would express "A has an instance of B".
The former expression is always redundant when repeated: "A is related to B" - OK, we knew that. The latter isn't: "A has another instance of B". So there are two distinct instances, which means that each instance has an identity. Identity is a core concept of entities.
So StandardPackageStandardMachine is just a regular class which represents a machine instance in a package, even though it's still a 'virtual machine'.
What you would normally do is create a master/detail relationship.
I'm not sure of the context of what you're doing. Typically your package would have a 1:many association of Package:Items, each item line would have it's own line number (sometimes called a sequence number), and it would have a foreign key relationship with product table. ie, something like this:
ID Name
1 Package A
2 Package B
ID PkgID Line(Seq) PrdID
1 1 1 1
2 1 2 1
3 1 3 2
4 2 1 1
5 2 2 2
ID Name
1 Monitor Y
2 Desktop Z
In this way, you can have as many of a single type as you want. The reason for the ID vs Line is that Line number duplicates for each order, while ID is a unique key for the table. Alternatively, you can make a composite key of PkgID and Line.

How to have more than 1 many-to-many relationship

My problem is simple but I can't seem to get EF to understand my database schema (Oracle).
Problem
I want to model more than 1 M-M relationship see diagram below:
As you can see I have a book and a journal - and I want to store a URI against both of these, by way of using a linking table (junction table, intersection table, you get it!). In this case it happens to be "www.google.com" that I want to store against Journal 16 and Book 75, they would be stored in the linking table in the follwoing format marked out in blue. For completeness I have added a green example for link with an ID of 5.
So you can see how the data knits together, I think you'll agree it's nothing too crazy.
Anyway, when I try and update EF model, it complains (in Visual Studio) with an error along the lines of.
Problem in mapping fragments starting at line 210:Foreign key constraint 'REF_URI_JOURNAL_FK1' from table REF_URI (REF_ID) to table JOURNALs (REF_ID): The columns of table REF_URI are mapped to AssociationSet BOOK_URISet's End BOOKs but the key columns of table JOURNALs are not mapped to the keys of the EntitySet BOOKs corresponding to this End.
Yeh - not really getting that error message!
I can get EF to work with 1 M-M relationship, e.g.
and it all works "hunky dory", but when I added a second M-M relationship it fell over with that error message above e.g.
Anyone know how I can get round this?
For two many-to-many relationships your need two linking tables (junction tables, intersection tables) REFBOOK_URI and REFJOURNAL_URI. I think that is what the exception is saying in a cryptic fashion. If you only had one table it would mean that if Journal 123 links to URI 789 then also Book 123 must also link to URI 789 because the REF_ID column in the link table would be a non-nullable foreign key to both the Journal and the Book table.

Generic Lookup table using fluent nhibernate

I can't figure out how to map a table structure like below within fluent nhibernate. Based on how I have the data laid about below you can see that there is no direct relationship between the Transaction or Member table and the CodeSet table. The id goes directly to the CodeValue table. So inside code you will find hardcoded values for the CodeSet tables. This is old code and I don't know if I should just change it or if nHibernate can deal with it.
I have the following tables:
Generic Lookup tables:
CodeSet
ID
Name
Display
CodeValue
ID
CodeSetId
Name
Display
ReferenceValue
I then have tables like
Transaction
ID
TransactionTypeId
Name
Member
ID
FirstName
LastName
MemberTypeId
The TransactionTypeId and MemberTypeId relates back to the CodeValueId on the CodeValue table.
So the data would look like:
CodeSetId Name Display
1 "TransactionType" "Transaction Type"
2 "MemberType" "Member Type"
CodeValueId CodeSetId Name Display ReferenceValue
1 1 ER Visit ER Visit 100
2 1 Surgery Surgery 200
3 2 Doctor Doctor 500
4 2 Patient Patient 600
TransactionId TransactionTypeId Name
1 1 Some ER Transaction
2 2 Some Surgery Transaction
MemberId FirstName LastName MemberTypeId
1 Betty Boo (Doctor) 3
2 Sammy Sue (Patient) 4
I suggest you read or walkthrough the 'Your first project' in the Fluent NHibernate 'Getting Started' document.
The retail company example has a very similar structure to what you're trying to implement.
https://github.com/jagregory/fluent-nhibernate/wiki/Getting-started

ASP.NET C# problem with Table and SQL logic

Hey I have 3 tables called STUDENT, COURSE and ENROLLMENT. STUDENT has a PK of S_ID(student ID) and COURSE has PK of C_ID(course ID). In enrollment it only has S_ID and C_ID. I have an object data source to show all the students name (in text, and S_ID as the value) in a drop down menu and it will show which courses he is registered in when clicked, using a datagrid and another object data source. I wont to have the student to have multiple courses to be registered too, but I cant do that because you cannot have the same ID in the COURSE table, so every student is only registered to one course.
Is there some sort of option to have same ID's in a table?
If not, then I must some how manipulate the string in C_ID in the COURSE table because all the courses start with ISDVXXX or ITSXXXX or HGFXXXX. This may be hard to understand but hopefully someone will help.
An example may help
So if a student named Joe with a S_ID of 123 is registered to ISDV, he will be registered to all the courses that start with ISDV. But my problem is that my COURSE table has to have unique ID for each course such as ISDV123, ISDV346, ISDV395 etc... so this also ruins my enrollment table because I cannot simply have ISDV in there, it needs a specific course but he is registered to all of them. Any more clarification will be given :P Thanks...
What you're trying to solve is a multi-valued attribute problem. Basically, you have two tables where one (students) has a primary key which is a foreign key in another table (classes). You don't want to have multiples of the same class in the classes table, but you do want a student to be able to have multiple classes.
So, there is a very simple fix, you create another table which contains at least these two columns: student_id and class_id. This way, you could have a single class that multiple students are linked to, and also multiple classes to which a student can be linked to.
What you are looking for is a many to many relationship - i.e. a single student can have multiple courses, and a single course can have multiple students. So your link table (is this what you intended enrollment for?) should have two columns, one for the course ID and one for the student ID.
So if you had student 123 and student 234, and courses ABC and XYZ, your table would look something like:
S_ID C_ID
123 ABC
123 XYZ
234 ABC
Now, for your PK on enrollments you could either use a composite key, or add a unique integer RowId (Identity or HiLo algorithm).
In that case, your enrollment table would look something like this:
S_ID C_ID RowID
123 ABC 1
123 XYZ 2
234 ABC 3
Then, to see what classes a student was in, you could do something like
Select * from courses c
inner join enrollments e
on c.C_ID = e.C_ID
AND e.S_ID = #StudentId
Your Enrollment table stands for the m:n relationship between the Studend and Course tables.
For instance your student with S_ID = 69 is Enrolled to the Courses with C_ID = ISDVXXX , C_ID = ITSXXXX, C_ID = HGFXXXX
A Student can be Enrolled to more Courses and more Students can be Enrolled to the same Course, that's why you have your Enrollment table. In our example, the Enrollment will have the following rows:
(69, ISDVXXX), (69, ITSXXXX), (69, HGFXXXX).
If later a student with the S_ID = 96 joins to the Course with C_ID = ISDVXXX, the following will be the new rows of the Enrollment table:
(69, ISDVXXX), (69, ITSXXXX), (69, HGFXXXX), (96, ISDVXXX).
The important thing to understand here is that each row in the Enrollment table stands for a Student Enrolled to a Course and there is no need to other fields than the ID of the Student and the ID of the Course, for these two fields together identify an Enrollment.

Categories