My code is working fine. However, I would like to stop the entity framework returning all the fields for the tblUser since I will only be using the ID, FirstName and LastName fields.
Now in my OnQueryCreated event handler I am only returning tblUsers that have a record in another table called tblWorkgroup. This is the envent handler:
For serious EF question you would be best served asking in the
EF forum but from a DD perspective I woudl create a view and map that in the EF model adding relationshiped etc. in the designer, this may not work for insert if other fields are required.
See my blog C# Bits | Twitter @sjnaughton Always seeking an elegant solution.
Is that the only way to bring the fields I need? I would not like to create stored procedures.
I was using a Select attribute in my EntityDataSource before like this:
Select="it.[UserId], (it.[FirstName]+' '+it.[LastName]) as FullName, it.[FirstName], it.[LastName]"
But as soon as I added the onquerycreated="UsersEntityDataSource_QueryCreated" the code broke.
I also tried to return an anonymous type from the onquerycreated event handler
but since the EntityDataSource is configure to use the tblUsers entity set it did not work
e.Query = from u in users
where u.tblWorkgroups
.Any(wg => wg.tblUsers.Any())
select new
{
u.UserId,
FullName = u.FirstName + " " + u.LastName,
u.FirstName ,
u.LastName
};
Furthermore, I tried casting it to a tblUser but it also does not work.
e.Query = (from u in users
where u.tblWorkgroups
.Any(wg => wg.tblUsers.Any())
select new
{
u.UserId,
FullName = u.FirstName + " " + u.LastName,
u.FirstName ,
u.LastName
}).Cast<tblUser>();
Is there anything else I could try since I do not want to use stored procedures?
Views or Stored Procedures is the only easy ways (I am sure the asp.net team could come up with others) I avoid SPROCS as I have found bugs with SPROCS in EF I prefer views if I need to do something like you are asking as I can at least relate a view to
other entities.
See my blog C# Bits | Twitter @sjnaughton Always seeking an elegant solution.
Marked as answer by pallone on Feb 02, 2012 09:21 AM
pallone
Member
165 Points
160 Posts
Stop Entity Framework returning some properties to EntityDataSource
Jan 31, 2012 03:01 PM|LINK
Hi,
My code is working fine. However, I would like to stop the entity framework returning all the fields for the tblUser since I will only be using the ID, FirstName and LastName fields.
<asp:DropDownList ID="ddlUsers" runat="server" Width="160px"
DataSourceID="UsersEntityDataSource" DataTextField="FullName"
DataValueField="UserId" AutoPostBack="True"
>
</asp:DropDownList>
EntityDataSource:
<asp:EntityDataSource ID="UsersEntityDataSource" runat="server"
ContextTypeName="MARSPermissionsManager.DAL.MarsMedicalEntities" EnableFlattening="False"
EntitySetName="tblUsers"
OrderBy="it.[FirstName], it.[LastName]"
Where="it.CompanySubscriberId = @CompanySubscriberId"
onquerycreated="UsersEntityDataSource_QueryCreated"
>
<WhereParameters>
<asp:ControlParameter ControlID="lstCompany" Name="CompanySubscriberId"
PropertyName="SelectedValue" Type="Int32" />
</WhereParameters>
</asp:EntityDataSource>
Now in my OnQueryCreated event handler I am only returning tblUsers that have a record in another table called tblWorkgroup. This is the envent handler:
protected void UsersEntityDataSource_QueryCreated(object sender, QueryCreatedEventArgs e)
{
var users = e.Query.Cast<tblUser>();
e.Query = from u in users
where u.tblWorkgroups
.Any(wg => wg.tblUsers.Any())
select u;
}
Partial class to add a FullName property for the DataTextField
namespace PermissionsManager.DAL
{
public partial class tblUser
{
public string FullName { get { return FirstName + " " + LastName; } }
}
}
Could someone please shed some light on how achieve that?
Cheers
C
sjnaughton
All-Star
27308 Points
5458 Posts
MVP
Re: Stop Entity Framework returning some properties to EntityDataSource
Jan 31, 2012 04:26 PM|LINK
For serious EF question you would be best served asking in the EF forum but from a DD perspective I woudl create a view and map that in the EF model adding relationshiped etc. in the designer, this may not work for insert if other fields are required.
Always seeking an elegant solution.
pallone
Member
165 Points
160 Posts
Re: Stop Entity Framework returning some properties to EntityDataSource
Jan 31, 2012 10:35 PM|LINK
Hi sjnaughton,
Thanks for your reply.
Is that the only way to bring the fields I need? I would not like to create stored procedures.
I was using a Select attribute in my EntityDataSource before like this:
Select="it.[UserId], (it.[FirstName]+' '+it.[LastName]) as FullName, it.[FirstName], it.[LastName]"
But as soon as I added the onquerycreated="UsersEntityDataSource_QueryCreated" the code broke.
I also tried to return an anonymous type from the onquerycreated event handler but since the EntityDataSource is configure to use the tblUsers entity set it did not work
e.Query = from u in users
where u.tblWorkgroups
.Any(wg => wg.tblUsers.Any())
select new
{
u.UserId,
FullName = u.FirstName + " " + u.LastName,
u.FirstName ,
u.LastName
};
Furthermore, I tried casting it to a tblUser but it also does not work.
e.Query = (from u in users
where u.tblWorkgroups
.Any(wg => wg.tblUsers.Any())
select new
{
u.UserId,
FullName = u.FirstName + " " + u.LastName,
u.FirstName ,
u.LastName
}).Cast<tblUser>();
Is there anything else I could try since I do not want to use stored procedures?
Cheers
C
sjnaughton
All-Star
27308 Points
5458 Posts
MVP
Re: Stop Entity Framework returning some properties to EntityDataSource
Feb 01, 2012 11:40 AM|LINK
Hi Pallone, you can't use projection with DD as that simply creates an anonymouse type which will not match anything in the metamodel.
Always seeking an elegant solution.
pallone
Member
165 Points
160 Posts
Re: Stop Entity Framework returning some properties to EntityDataSource
Feb 01, 2012 12:01 PM|LINK
Hi sjnaughton,
Thanks. I realised that after I tried it.
This is one of the questions I had above:
Is using views or stored procedures the only way to bring a limited number of fields I need on the client?
I would not like to use stored procedures or views and was wondering if I there is any other way to achive this.
Cheers
C
sjnaughton
All-Star
27308 Points
5458 Posts
MVP
Re: Stop Entity Framework returning some properties to EntityDataSource
Feb 01, 2012 01:45 PM|LINK
Views or Stored Procedures is the only easy ways (I am sure the asp.net team could come up with others) I avoid SPROCS as I have found bugs with SPROCS in EF I prefer views if I need to do something like you are asking as I can at least relate a view to other entities.
Always seeking an elegant solution.
pallone
Member
165 Points
160 Posts
Re: Stop Entity Framework returning some properties to EntityDataSource
Feb 05, 2012 09:42 PM|LINK
Hi,
Just to let everyone know thre is another approach called table splitting. This will allow you to bring only the properties you need.