开发者

Does the EntityDataSource support "it.Property.Property" syntax?

开发者 https://www.devze.com 2023-01-03 02:41 出处:网络
I have an EntityDataSource where I\'m trying to replace some previous code-behind work. My EntityDataSource looks like:

I have an EntityDataSource where I'm trying to replace some previous code-behind work. My EntityDataSource looks like:

    <asp:EntityDataSource 
        runat="server" 
        ID="personDataSource" 
        ContextTypeName="Model.GuidesEntities" 
        EntitySetName="CharacterFavorites" 
        OrderBy="it.Person.FullName" 
        Select="it.Person.Id" 
        Where="it.UserName = @userName" />

When when I actually use it I get the error:

'Person' is not a member of type 'Transient.rowtype[(Id,Edm.Int32(Nullable=True,DefaultValue=))]' in the currently loaded schemas.

Does the EntityDataSource not support walking the relationships? How would you do this with 开发者_JS百科the EntityDataSource?

Also the @userName parameter is being added in the code behind for now. Extra points for anyone who knows how to specify a username parameter directly in the WhereParameters collection.


EF uses "super lazy loading" here (my own term). It doesn't automatically reach out and grab the associated "Person" object. You MUST specify inside the entotyDataSource what other objects to make available. So you need to add this line: "Include="Person". See below:

 <asp:EntityDataSource 
        runat="server" 
        ID="personDataSource" 
        ContextTypeName="Model.GuidesEntities" 
        EntitySetName="CharacterFavorites" 
        OrderBy="it.Person.FullName" 
        Select="it.Person.Id" 
        Where="it.UserName = @userName" 
        Include="Person"  />

If you need to add multiple associations, separate them with commas: Include="Person, Dog, Chicken, SalesOrder" Also, it's "==" not "=" in your Where clause.


There is no standard predefined parameter type which allows you to get the current user name as a Where parameter in markup (I guess you are talking about User.Identity.Name). Something simple like...

<asp:Parameter Name="userName" DefaultValue='<%# User.Identity.Name %>' />

...doesn't work unfortunately since you cannot use data binding syntax in the parameter controls.

But you can create custom parameters. (On the second half of that page is a concrete example of a "user name parameter".)

To your problem with the exception: I'm using the "it-Syntax" with navigation properties a lot in EntityDataSource and it looks more or less exactly like your example. Without seeing more of your model it's hard to tell what might cause the error. But generally, navigating through properties in EntityDataSource is supported and works.


   protected void MyNotesSelecting(object sender, EntityDataSourceSelectingEventArgs e)
            {
                e.DataSource.Where = "it.UserName=="+User.Identity.Name
            }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号