Linq to entity navigation properties - entity-framework

I need to get the 3rd level entities using navigation properties.
table1
table2
table3
table4
table5
Table 1 relates to table2 and table 2 relates to table 3,4,5 with foreign key relation ships.
I want to pull the data from table 3,4,5 based on the table1 fields. I tried to use the include method but unable to find the method. If you can give the sample code that would be very helpful.

Use the include method in System.Data.Objects. Include should work with the .Include("Table1.PropertyNameTable2.PropertyNameTable3.PropertyNameTable4"); notation.
http://msdn.microsoft.com/en-us/library/bb738708.aspx

Related

PostgreSQL: how to create a trigger that runs after adding a new row in one table and rows related in another table?

I have two tables, e.g. TABLE1, TABLE2. They are joined by "one-to-many" relationship (many records from TABLE2 are referencing to every record in TABLE1). I want to make a trigger that runs just after adding a new row in TABLE1 and all the rows related with it in TABLE2. How to solve it? In particular, how to "inform" the trigger that all related rows from TABLE2 are added?

how to establish a relationship between two columns in one table

I want establish a one-to-one relationship between two columns (a program code and a test code) in the same table. I want all tests with the same test code to have the same program code.
My first thought was to use a UDF to find cases where the same test code corresponds to two different programs. I learned that this won't work because t-sql only checks UDFs in check constraints after INSERTS -- not after UPDATES
why is t-sql allowing me to violate a check constraint that uses a UDP?
My next thought was to move the logic from the UDF into the check constraint itself. But t-sql is saying that sub-queries are not allowed in the check constraint. This also means I can't use EXISTS syntax (which I think also uses a sub-query).
ALTER TABLE [dbo].[mytable] WITH CHECK ADD CONSTRAINT [oneProgramPerTest] CHECK
(
(select COUNT(*)
from mydb.dbo.mytable u1
inner join
mydb.dbo.mytable u2 on u1.testcode=u2.testcode and u1.progcode <> u2.progcode
)=0
)
Unless there is some way to enforce this logic without (1) a udf or (2) a subquery then it seems like I need to create a "dummy" table of program codes and then enforce a one-to-one relationship between test codes from myTable and the dummy table. This seems really ugly so there has got to be a better way. Right?
Have you read about normalization (and if you haven't why are you designing a datbase?). You should havea structure with
tableA
id (PK)
programcode
other fields
tableB
programcode (PK)
testcode
Add a formal foreign key between the two tables and define program code as the PK in tableb.
Then to get the data you want:
select <Name specific fields, never use select *>
from tableA a
join tableB b on a.programcode = b.programcode

Entity Framework Discriminated Associations

Is there any way of getting discriminated associations to work in Entity Framework 4? That is, where we have the following tables
TableA
RelatedEntityTypeId
RelatedEntityTypeKey
TableB (1)
Id
TableC (2)
Id
TableD (3)
Id
and I want to have three associations on the entity for TableA:
TableB
TableC
TableD
which are defined by the RelatedEntityTypeId and RelatedEntityTypeKey fields...when RelatedEntityTypeId = 1, then the association is to EntityB, when RelatedEntityTypeId = 2, then the association is to EntityC, etc.
Thanks.
I don't know your purpose of doing this. I have used following approach to solve similer problem.
You can define a base type for all three tables (A,B,C). And when you want retrieve a object use a generic method for all tables (which returns a base object). And then you can check the type of the returned object to get the A,B,C object.
TableBase
Id
TableB (1):TableBase
TableC (2):TableBase
TableD (3):TableBase

Replace Text of a field from a different table in SQL

I have two data buckets using a cryptic naming convention.
Am I able to update the data in the fields on the main table where the record entry is equal to the primary key on the other table?
Something like Table1 has 5 columns, t1A t1B t1C t1D t1E
and Table2 has 2 columns description, and Table1code.
Am I able to switch the data in Table1 with the description field in Table2?
I have tried doing a sql update/case statement but kept getting non-boolean errors when I would run it.
Any help would be appreciated.
You need to do an update with a join. Have a read of this http://www.bennadel.com/blog/938-Using-A-SQL-JOIN-In-A-SQL-UPDATE-Statement-Thanks-John-Eric-.htm

EF4 VS2010 not mapping linked tables properly

I have a pretty standard linked table setup
Table1
Id - PK, seeded
Table2
Id - PK, seeded
Table3
Table1Id - FK
Table2Id - FK
For some reason, when using the designer to create the edmx in VS2010, table 1 exists already. When adding Table2 and Table3, they get added, but no relationship gets attached between Table3 and Table1. As well, the designer usually does not include the linking table which is what I would like to continue to do as well. Anyone know why this would be occuring? The existing edmx is quite large and I'd prefer not to delete everything and re-add!