I have an EF code first application. I have a column PersonId that I need to join with another database (warehouse) table Persons which contains all the people in our organization.
I have read that one solution is to use a view then make my entity model from the view but this seems quite painful if we have a PersonId column in many tables.
How best to return back all rows from table A joined with a table B from another database on key column PersonId, really to return PersonDisplayName from table B?
There has got to be a best practice for this situation?
Related
I have a database with many tables. Two of them are like this :
Table A
{
id int,
fkTable int,
fkClass int
}
Table B
{
id int,
reference varchar(100)
}
The A.fkTable field is a foreign key on another table, without particular constrains. We can only know on which table the relation is by B.reference field. The A.fkClass field point to the B.id field.
For example, if B.reference = "Operations", we can determinate that A.fkTable will point to Operations.id. We talk about 20 possibilities for B.reference value.
I know it's possible for Entity Framework to retrieve an specific entity by a field value, but I know only the way by a field in the same table.
Is there a solution in order to avoid manual joining in the code and EF retrieve the good mapped-object in function of B.reference value ? I think I need to declare manually my 20 entities in order to explain to EF the relationship between fkClass and the good table, but i'm not sure how coding the switch case.
And I don't want to do two queries : one in order to get the reference, and another in order to join fkTable to the good entity.
Edit : may can I use HasDiscriminator ?
Edit2 : I can't modify the database. I can only modify my C# project which connects to the database. It's not code-first. It's model-first.
When I write C# applications, I use to write sql relations as inner join for example, in the query:
select xxx from TableA as A inner join TableB...
I don't really see why I should define these realtions (hard defined) in Management Studio.
Should I, and why if required?
Regards
Two main reasons and a minor third one: data-integrity and performance - and documentation.
Performance is simple: if you define a relationship between two tables you will normally automatically create an index on those two columns, and your database will use those indexes to speed up look-ups when they can be used.
As for data-integrity, even though you leave the important part of your join out of your example, you assume in a join that a foreign key field can only contain values that exist in the primary key field. Your database can make sure of that, and will make sure of that when you define those relations in SQL server.
If you do not define those relationships, you could easily create a situation where you have, say, Orders, that belong to Customer 12345, who does not exist in your Customer table.
Or you could delete Customer 23456, leaving all their Orders in your system, but without an existing Customer.
A final reason is documentation: your database is not only made to be accessed by your code. If someone else accesses your database and sees only unconnected unrelated tables, how will they know that field cstID in table CstOrderHdr happens to be a reference to field id in table Relations where Relation.RelTyp = 'Customer'? Who is to stop them from filling in 0, null or a random number?
I have two tables, one containing patient information, the other, the notes for each patient.
(One patient, many notes for a patient).
Given this, in the Designer (which you access by right-clicking on the chosen DataSet), how do I create a one-to-many relationship? I have never performed this before.
Secondly, for the patient notes table, how would I add a note to a patient record using SQL syntax? Note, this is not updating an existing one, but adding a completely new one to the patientNotes table using the unique patient ID number as the reference (so only that specific patient has that note added to them, not them and everyone else).
Very technically speaking, you don't need to do anything to create a one-to-many relationship. You just have to have the two tables set up as you have them and use them as you intend on using them. I work in data warehousing and unfortunately a great many of our relationships like this are not formalized with any sort of key or constraint.
The correct way to do it is to implement a foreign key constraint on the patient ID column on the patientNotes table. A FK will only allow you to insert data into patientNotes IF the patient ID exists in the patient table. If you would try to insert a note into your table that has a patient ID that doesn't exist in the patient table, the insert would fail and the SQL engine would give you an error. Note that the column on the patients table that you are creating the FK to must be a primary key.
Inserting data will really go as any other insert would:
INSERT INTO dbo.patientNotes (patientId, NoteText)
VALUES(4265, 'During his 8/14/2014 visit, Mr. Cottinsworth complained of chest pains. Evidently he has been wearing a lady''s corset to hide his large gut. Advised the very portly Mr. Cottinsworth to discontinue corset use'
You could toss that in a SP, put it in your code and use parameters for the patientId and NoteText, however you wanted to do it.
As far as doing this all in Visual Studio graphically, I can't be of much help there. I typically use the TSQL editor and type out what I want to do to the DB. I'm sure there are tutorials abound on how to set up FKs on Visual Studio.
Further reading:
http://msdn.microsoft.com/en-us/library/ms189049.aspx
http://www.scarydba.com/2010/11/22/do-foreign-key-constraints-help-performance/
what are the advantages of defining a foreign key
I've been used to this kind of convention before starting to work with Entity Framework/MVC:
tblMyItem (id, id_lookup1, val2, val3, val4...) - single, generic table
tblkLookup1 (id, val1, val2,...) - one to many relation table
tblmMyItemLookup2 (id, id_myitem, id_lookup2) - many to many relations table.
Recently I've found somewhere on the web that it's not good to create id column in tblmMyItemLookup2 when using Entity Framework, but I couldn't find more information on that. Could anyone please explain, why this is so significant?
When designing many-2-many relationship(i.e Student - Teacher) based on intermediate table (Student2Teacher) with only 2 foreign key columns, you'll end up having entities with navigation properties without intermediate table(no entity will be created for that table in context at all):
Student.Teachers <- EntityCollection<Teacher>
Teacher.Students <- EntityCollection<Student>
Meanwhile, if you add any extra field to your intermediate table, you'll have navigation properties pointing to intermediate entity:
Student.Student2Teacher
Teacher.Student2Teacher
making your querying uselessly more complex
In general we use lookup tables to macth associated records in two different tables that have a many to many relationship. For instance, let we have three tables: Students, Professors and StudentsProfessors. Since, a student can atten lesson that are serviced by many professors and a professor can teach more than one leasons then this is clearly a many to many relationship. So the lookup table called StudentsProfessors is used to match a student to her/his professors and vice versa. Now, the use of an id for each record of this lookup table is meaningless. We are not going to use this number anywhere. We just need to know that Student with studentId=10 is associated with professors with ids in (1,2,4,9). Just that and not the id of this record in the lookup table.
I have three table in SQL database such as table Person has relation one to many to table member and number has one to many relation to table member .that means member table in this design is junction table and i want do many to many relation between person table and number table for removing member table .but i cant because when i do relation between person and number in my EDMX file and Generate the EDMX model to SQL database .the sql database creates a table between person table and number table has two field such as personId and numberId. i want add Some field to junction table that removed between two tables.for example i need firstname or last name in junction table that i removed .how i alternative a junction table to many to many relation between two table in my EDMX file that the junction table has more field?
If you are using database first simply add all columns you need in junction table and update model from database. EDMX will contain a new Member entity.
If you are using model first you cannot use many to many relation between Person and Number. You must model three entities - Person, Member and Number with the same relations you expect in the database. Once you use generated database from model you will get the database you expect.
Many-to-many relation with hidden junction table in EDMX is only for scenarios where you have a real junction table without any additional data columns.