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
Related
I have two tables, shops and products. Table shop with columns shopId (PK) and shopName. Another table Products has columns productId (PK) and productName. Because I have a relationship many-many between two tables I create another table shopProduct with two columns shopId and productId.
For example I have shop1 with id=1 and I have shop2 with id=2, product1 with id=1 and product2 with id=2. and the records of the table shopProduct is 1-1 ,2-2.
But now I want to have a product3 with id=3 that apply to all shops.
I consider in the real problem that i have about 250 shops.
How can implement this without a stored procedure?
I don't want to do of course 250
insert into shopProduct (shopId, productId)
values (#shopId,#productId).
Can I do for loop for example to feed the the shopId value every time?The other value for product id is the same every time.
From my understanding of this question, try this... Seems too simple, but...
Insert into ShipProduct (ProductID, ShopID)
Select 3, ShopID
From Shops
I have table called customers contain custID,custName, another table called products contain proID,proName,price and third table Cust_PRo contain id,custID,proID.
i filled the products with data like this:
proID proName price
1 potato 100
2 cotton 600
3 rice 200
and in another form i have combobox i filled it with products names and textbox to write the customer name and gridview to appear data and there are two buttons one to add the values into grid and second to save it into database.
when i write the customer name and choose the products the data appear in the grid like this:
custName ProName
john potato
john cotton
john rice
as you see one customer can take many products the problem is to add the values into database i want when the user click the save button the data insert into the database like this:
first customers table:
custID custName
1 john
second Cust_PRo table:
id custID ProID
1 1 1
2 1 2
3 1 3
thank you
i'm not sure i got you 100% but i think you need some algorithm or something.. anyway
first, you need to generate a CustID for your new customer, to do that you need to get the maximum CustID from the table Customers and add it to 1 to avoid Primary key violation.
select max(CustID) from Customers
then, you'll be good to go. You have (CustID and ProdID from comboBox1.SelectedValue;)
I added the values to datatable and then used the SqlBulkCopy class to insert these values into database
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.
I have a dropdown getting populated by fetching a query on the database.
Say it fetches items given as follows:
Teacher,
Student,
Pricipal,
Sweeper,
FinanceManager.
While showing it in the dropdown, I wish to show it in the following order:
Principal,
FinanceManager,
Teacher,
Student,
Sweeper.
This isn't any specific order (ascending or descending), but just a order that has some relevance according to the personalities.
How do I acheive ?
Since you fetching the data from database,the easy solution to me is adding a extra column to the same table which hold order.
eg. Your Table
Column1 Column2 .... DisplayOrder(int)
Principal 1
FinanceManager 2
etc...
So you can order by the DisplayOrder when you do the selection
you could give them an extra "role-flag" in your db
ID NAME FLAG
1 Principal 5
3 FinanceM 4
33 Teacher 3
22 Student 2
99 Sweeper 1
and you could do the query by "order by FLAG"
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.