Entity Framework, ado.net Data Services, odata - entity-framework

I'm very new to Entity Framework, that's my disclaimer! I have a SQL 2008 database with 2 tables, tblModel and tblHairColor. tblModel contains a column named hairID which is a foreign key to the tblHairColor table's primary key of id.
I created the ado.net entity data model and now, following http://msdn.microsoft.com/en-us/library/dd728283.aspx, trying to access my data resources created.
My URL of http://localhost:51157/WcfDataService.svc/tblModels(1)/modelname/$value works great by returning the model's name (of record 1) from the tblModels table. However, when I try to access the hair color via http://localhost:51157/WcfDataService.svc/tblModels(1)/modelname/tblHairColor it does not work, (http 404 not found).
My entity model, generated from my SQL database, created a tblModels navigation property in tblHairColor and a tblHairColor navigation property in tblModel. It also auto generated an association of tblHairColor to tblModel (1 to *). I expected 1 to 1.
My question is what needs to be added/changed to allow this query, http://localhost:51157/WcfDataService.svc/tblModels(1)/modelname/tblHairColor, to return the models hair color?
Thanks in advance for your time.
Bob

modelname should not be used in URL, just navigational property:
http://localhost:51157/WcfDataService.svc/tblModels(1)/tblHairColor
If you want both model and haircolor, you should use $expand:
http://localhost:51157/WcfDataService.svc/tblModels(1)?$expand=tblHairColor

Related

Entity Framework 6 - Inserting/Updating 2 table joined by a view

In my database I have
a Members table which contains basic member details (MemberId [primary key - auto generated number], MemberName, IsActive).
a MembersDetails table which contains more detailed information about the member (Address, Phone, Birthday ...). MembersDetails has MemberId field as a foreign key to the Members table.
There's a reason (part of the app logic) that the 2 tables are separated and are not all in one table.
I've created a view that gets a full member details (a join of the 2 tables), and Entity Framework created an object that represents the view.
I have 2 questions:
Is there a better way of flattening 2 joined tables into an object other than creating a view in the database?
I would like to create an object of the view type (a full user details), initialize it's properties and insert it to the database (which will put the info it needs in the Members table, the the generated id, and than insert to the MemberDetails table). Is there a way to do that?
Ad 1.
I think a database view will be a good choice for performance reasons.
But you can investigate an inheritance provided with the Entity Framework.
It allows you join two separated tables in one model object containing all properties (from "derived" and "base" table). Note, it will be OK for one to one relations (but not for one to many).
Implementing Inheritance with the Entity Framework 6 in an ASP.NET MVC 5 Application
Ad 2.
The Entity Framework inheritance will help you with this issue well. Alternatively, if you can use a database view, just create stored procedures for inserting and updating data included in a view, then map the stored procedures for specified actions on the view model generated by Entity Framework.

Entity Types not mapped but stored

I've a problem mapping just one table from database, I add several tables using "Update model from Database" function through Visual Studio 2010 interface, and everyone works as expected except one table!
Looking at "Model Browser" I can see the table doesn't appear under "Entity Types" but it is present under section "ObjectEntity.Store", so my POCO Generator create an entity related and I can't try to add it again from database but I can't access it through context (like context.table_name).
Tha table doesn't have particular form, there are just two key fields
Could someone help me?
Thanks
I guess it is junction table for implementing many-to-many relation, isn't it? In such case it is correct behavior. EF will hide this table because it is not needed in object oriented approach where many-to-many association can be modeled directly without helper entity. You will see in your model that those two related entities are connected by line with * - * multiplicity and each entity will contain navigation property which is collection of related entities. By manipulation with entities in these collections you are creating or removing records in that hidden table. That is the way how you work with such relation in EF.

Model a 1-to-many relationship with a single table in Entity Framework

Lets say I have 2 tables on my physical model, Receipt(ID, Location) and LineItem(ID, ReceiptID, ItemName) where a Receipt has multiple LineItems and ReceiptID is a Foreign Key to Receipt's ID.
I want to model these as a single table in my conceptual model, where I only see a table of LineItems with the Location included on each LineItem.
Every time I try to model this in the Entity Modeler, I get an error about how the Primary Key must be the same for every table being combined into the single conceptual entity.
Is this even possible to model using the entity framework?
Thanks!
No there is no way to model this directly. You must either create database view and map that view or import both entities and create QueryView in the model. In both cases resulting entity combining your two tables will become readonly and the only way to support CUD operations will be mapping stored procedures.

Removing Entities from Database table via Navigation Property using RIA Services and Entity Framework

I have 3 normalised tables consisting of Employees, Departments and EmployeesToDepartments. I wish to be able to assign an Employee to one or more Department, hence the link table (EmployeesToDepartments). I can successfully query the database and extract the full hierarchy of entities via the Navigation properties using
this.ObjectContext.Employees.Include("EmployeesToDepartments").Include("EmployeesToDepartments.Department")
plus the [Include] attribute in the metadata, thus allowing me to access the Departments for a given Employee. Upon trying to remove a link between an [Employee] and [Department] in the [EmployeesToDepartments] table I was given a Foreign Key Constrain error.
I have simplified my model to include just one navigation property between [Employees] and [EmployeesToDepartments]. A Foreign Key constraint between[Employees].[ID] and [EmployeesToDepartments].[IDEmployee] was preventing me from updating the EmployeesToDepartments table. With this removed via a Relationship setting I can now update the table. I can now execute the following code
foreach (var rel in _employee.EmployeesToDepartments)
{
_employee.EmployeesToDepartments.Remove(rel);
}
_domainContext.SubmitChanges();
without error.
I was expecting to see the entries in the RelEmployeesToDepartments with the IDEmployee to have been deleted. What I see in the table are the value 0 where the IDEmployee previously was.
Is it possible to force a DELETE statement to be issued? Am I misunderstanding the basic concepts here?
Any help would be much appreciated.
Removing entities in navigation property only breaks the link between entities. You have to delete from the EntitySet to achive what you want.
ex)
myDomainContext.EmployeeDepartments.Remove(employeeDepartmentToRemove);
myDomainContext.SubmitChanges();

Adding a property to an Entity Framework Entity from another table

I'm just starting out with the Entity Framework and ADO.NET Data Services and I've run into an issue that I can't seem to figure out. I have two tables, one that has user information and the other that has a created by field. Within the database, there isn't a foreign key between these tables. The user table contains an arbitrary Id, a username, and a display name. The created by field contains the user's username. In my entity I would like to have the user's display name since this is what I need to display and expose over the ADO.NET Data Service? I'm aware that I could restructure the database, but I was hoping that I could do the join using the username as I would in a SQL statement.
Thanks in advance,
-Damien
You can make a view using a join of both tables, and then use this object to display the user's name.
There's some info on mapping custom queries here.