Adding a property to an Entity Framework Entity from another table - entity-framework

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.

Related

When I get a value through a navigation property does entityframework run an sql query or does it already have the data?

Lets say there is a User entity, and there is a Group entity, User has a GroupId to relate it to a group, and there is a Group navigation property on the User entity.
I have retrieved 1 User through EF. when I do user.Group.Name does EF run a sql query to join it with group to get the name of the group, or did it get that data when I got the user object?
Entity Framework supports three ways to load related data.
Eager Loading
Lazy Loading
Explicit Loading
A quick read of the following blog could be of help http://blogs.msdn.com/b/adonet/archive/2011/01/31/using-dbcontext-in-ef-feature-ctp5-part-6-loading-related-entities.aspx

Joining list with table in Entity Framework

I am using Entity Framework with asp.net mvc3 razor. Now I have a table which represents Countries (like India, US) etc. And my requirement is I need to open a pop-up with the flags of all countries which I have in my database. And when user click on one flag I need to show that particular country details first and remaining as line by line in the webgrid(asp.net mvc3 razor)
So I prepared a list of "Countries" by getting all the countries from database. And I prepared another list OrderofCountries by adding Order(property) "1" to the which user had clicked. And from 2 to all the remaining countries. Now I need to join this list i.e "OrderofContries" with the remaining tables (from database as for the requirement). But Entity Framework raises an error:
Unable to create a constant value of type 'Slmg.BusinessObjects.CountriesBO'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.
My idea here is by using order property I can sort the data so that I can get the required data.
Can we join our prepared list with the database table in Entity Framework? How to solve my issue. Can any one please help me to find the solution.
No you cannot join list in your application with database table unless you pull all data from that table to your application by calling ToList or AsEnumerable.

Entity Framework, ado.net Data Services, odata

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

Entity Framework code first aspnet_Users mapping / joins

I was wondering with Entity Framework 4.1 code first how do you guys handle queries that involve an existing aspnet_Users table?
Basically I have a requirement for a query that involves the aspnet_Users so that I can return the username:
SELECT t.Prop1, u.Username
FROM Table1 t
INNER JOIN aspnet_User u ON t.UserId = u.UserId
Where t.Prop2 = true
Ideally in linq I would like:
from t in context.Table1
join u in context.aspnet_Users on t.UserId equals u.UserId
where t.Prop2 = true
But I'm not sure how to get aspnet_Users mapping to a class User? how do I make aspnet_Users part of my dbset ?
Any help would be appreciated, thanks in advance
Don't map aspnet_Users table or any other table related to aspnet. These tables have their own data access and their own logic for accessing. Mapping these tables will introduce code duplication, possible problems and breaks separation of concerns. If you need users for queries, create view with only needed information like id, user name, email and map the view. The point is that view will be read only, it will contain only allowed data and your application will not accidentally modify these data without using ASP.NET API.
First read Ladislav's answer. If you still want to go ahead : to do what you want would involve mapping the users and roles and members tables into the codefirst domain - which means writing a membership provider in code-first.
Luckily there is a project for that http://codefirstmembership.codeplex.com/ although its not a perfect implementation. The original is VB, look in the Discussion tab for my work on getting it running in c# MVC.
I'm working with the author on a better implementation that protects the membership data (password, last logged on date, all of the non-allowed data) but allow you to map and extend the user table. But its not ready yet!
You don't really need to use Entity Framework to access aspnet_membership provider accounts. You really just need to create an instance of the membership object, pass in a unique user identifier and a Boolean value indicating whether to update the LastActivityDate value for the user and the method returns a MembershipUser object populated with current values from the data source for the specified user.
You can then access the username by using the property of "Username".
Example:
private MembershipUser user =
Membership.GetUser(7578ec40-9e91-4458-b3d6-0a69dee82c6e, True);
Response.Write(user.UserName);
In case you have additional questions about MembershipProvider, you can read up on it on the MSDN website under the title of "Managing Users by Using Membership".
Hope this helps you some with your requirement.

Zend Framework Doctrine insert one-to-many

Amongst order tables i have a Customers table and Addresses table. A customer can have many addresses so I have setup a one-to-many relationship in a yaml file. The thing is the id for the Customers table is auto generated so I would not know the Customers_id until after the insert however, the Customers_id is a foreign key in the Addresses table.
The information for both tables is captured on the same form although each set of data is in a subform. How do I get Doctrine to insert the data into the Customers table then fetch the Customers_id just entered and use it as the foreign key for the Addresses table.
Hope I have been able to get the essence of the question across.
BTW I am using Zend Framework and Doctrine 1.2.3
Once you do $customer->save(), you may use $customer->id (if "id" is the Customers_id column name of your customer table) to get the Customers_id to put in Addresses table.
Once you do $customer->save(); the following statement $customer->identifier(); should give you the id.
Assuming you are using Doctrine 1.x you could do the following:
$cusomer->Address[] = $address; // Assign multiple addresses in this manner
$customer->save();
This will first save the customer details to the db and then store the address data. Since doctrine understands the relationship between customer and address, the ORM will insert the customer id in the address table. Further this will be run within a transaction by Doctrine which ensures that if one of the operations fail the entire transaction will be rolled back.