How to share table property between two tables in ADO Entity Data? - asp.net

i'm having a little problem in my project. i'm using ADO.Net Entity Data Model,
let's say i have 2 Tables:
Offices : a. id
b. Name
Requests: a. rid
b.fname
c.lname
d.mobile
i want the requests table will have a relations to the offices table that each row in requests will have the id of the one of the tables.
i tried to do 1 to many relations but it didn't work , i just couldnt add data to the table.
thanks for your guidence

Your Requests table needs to have a field to relate back to the offices table. Which typically should be named OfficeID or something similar. Add that field and create the relationship from Offices.ID to Requests.OfficeID and it will work fine.

Related

Mapping model to a SQL view

I've known how to map a model to a table in SQL in MVC using this:
[Table("table_name")]
I found this answer Take data from different tables and display it in View Index in mvc4
. The answer was to create a new model on joining tables in SQL then bind columns to model's fields.
However, can I make it easier by mapping my models to SQL views? Since views are considered to be (virtual) tables and they already contain what I need.
Yes you can map an entity to a view. EF doesn't know or care whether the object is a table, view, synonym or external table. You just need to map the entity to the view name, and declare the key columns. Views don't really have key columns, so just use whatever combination of columns uniquely identifies a row in the view results.

Why Updating From Database Does Not Bring All Tables from the Database?

When I update the model from database, I am not able to access to some of the tables as entity.
Through Model Browser, I can see those tables under MyDBEntities.Store (I don't know what it is) but under the Entity Types, these tables are not listed. Tried to downgrade to EF5, tried opening and updating the model with VS2012. Tried deleting and recreating the model so many times but no luck. If I right click on Entity Folder and select Add New Entity, under the Base Types, these tables are not listed either. All tables have PK and FK by the way. Anyone has a clue?
EDIT:
I just noticed, if remove the model and while updating it from database, if only add these 5 tables, they are being added properly. But this time caused the run-time error: Two entities with possibly different keys are mapped to the same row. Ensure these two mapping fragments map both ends of the AssociationSet to the corresponding columns.
When you add an association table that creates M:N relation between two other tables and has no additional attributes, then the association table is not shown in the model.
See http://smehrozalam.wordpress.com/2010/06/29/entity-framework-queries-involving-many-to-many-relationship-tables/.

Whats the most efficient way to create an orchestration which updates an Oracle database?

I am creating my first orchestration in Biztalk and am having trouble coming up with an efficient way to update a database (specifically, up to 3 different tables).
The user calls our service with an inbound message matching a schema which contains emplid (unique id) and then a bunch of name-value pairs (see source schema in this picture). The "name" corresponds to a column in a table (e.g. if the name is "employeename" it corresponds to the NAME column of the EMPLOYEE table). The value of course is the new value that the user wants that column to be updated to.
So they could pass in an update message which only applies to 1 table, 2 tables, or all 3, depending on the fields they want to update for the passed in employee.
I am currently approaching it as 3 separate updates with 3 table adapters (one for each table, one of which is pictured above) but Im having trouble working with the different cases of if they pass in updateValuePairs for all 3 tables, versus only one or only for two tables (the other queries still attempt to run and fail). Am I approaching this right? Or is there a better way to accomplish what I am trying to do?
i would try i different way in order to implement cleaner solution,
create a Store-Procedure that handle the logic to which table to go
than you will need only on mapping and one LOB adapter instead of the 3 you got now
over view solution
1.receive input in the orchestration
2.mapping the input to the Stored procedure generated schema
3.sending the mapped data to the DB/LOB adapter into the DB
here is a link that can help you (im assuming you use biztalk 2010):
How to use Oracle Stored Procedure

EF4.1 CodeFirst: Add field to join table

I am using EF 4.1 RC and CodeFirst/POCO to build my database by code.
Imagine having a many-to-many relationship like Teachers-Students (one teacher can have many students, and one student may have many teachers). Accordingly I have two POCOs: (1) Teacher and (2) Student.
When EF creates the corresponding tables you will end up with three tables: (1)Teachers, (2) Students and (3) an extra join table. The join table contains exactly two fields: a Teacher_ID and a Student_ID.
I was wondering if I had any chance to add an extra field to the join table, e.g. "Grade" (the grade a certain teacher gives a certain teacher)?
Currently I have no idea how to achieve this with only two POCOs.
So I guess all I can do is create a third POCO (for the join table) manually, am I right? That will certainly work, but then I am losing nice navigation properties like oneTeacher.Students.First(), etc. That is the main reason why I am still looking for another way.
That's correct, and does not only apply to Code-first. If you have extra fields in your joining table, you will have it mapped as an entity. And vice-versa, if you want an extra field in your joining table, you need to create a new entity and have zero-or-one-to many or one-to-many navigation properties to the Teacher and Student entities. In any case, you lose the comfort of accessing Teacher.Students and Student.Teachers and have to go via the intermediate entity.
Alternatively, you could think about modeling the DB structure differently and extracting the extra info into the Teacher or Student or a fourth entity. But that depends entirely on your scenario.
Yes, the join table cannot have a payload or you need to break it down to 2 one to many association which will result in creating a third entity to hold the PKs as well as the additional properties.
This is an idea I still haven't found time to try it out. Maybe you can keep your Student and Teacher classes as they are, and add a third POCO StudentGrade with properties Student, Teacher and Grade. Then you'll have to use the fluent API to make sure that both the many to many relation between Student and Teacher and the StudentGrade class map to the same table.
Hope that helps

asp.net MVC fetching data with table relations

I have created my Database which has:
Artist, tracks and TracksPerArtists
I can do sql queries through the entity model. For example when I make:
database.TRACK.ToList()
I get the list of track to be shown on index view for example. But my foreign keys come empty. While there is an artist and a track, and the correct row for ArtistsPerTrack, this item in my track.ToList() collection is empty.
Is there a different way to fetch those data?
I came from cakePHP framework in which you can define the Model.recursive property to declare the depth of the relations you want to fetch.
Is there anything similar here?
There is something similar.
If "database" is DataContext and Artist and Tracks have many to many relationship in database, you can fetch related entities using Include clause:
database.TRACK.Include("Artists").ToList()
where "Artists" is the name of property on Track entity.

Resources