Ticket T350312
Visible to All Users

EF - How to show data from a stored procedure in a ListView with Entity Framework 6

created 9 years ago

Is there a simple or  clear step on how to fill a list view from an SQLView or EXEcSproc on an EF code first project without XPO, either by a controller or an no persistent object , I have done it from Msaccess query with Xpo  and it seem simple enough, I would prefer running a SQLQuery with parameters  to select the information.

Answers approved by DevExpress Support

created 3 years ago

We published XPO and EF Core-powered code examples for the most popular scenarios with XAF's non-persistent objects and data coming Stored Procedures (SP):

A non-persistent class is a type of business class. XAF generates a UI for this class but does not bind it to an application’s database table. You can use this class to display a List or Detail View with temporary data generated in code or loaded from storage. You can also use it to display an empty View (dialog) and process the user’s input.

See Also

    Other Answers

    created 9 years ago (modified 3 years ago)

    Hello Boris,

    You can use the ObjectContext.ExecuteStoreQuery method to get a list of non-persistent objects from a stored procedure. To get an ObjectContext instance, use the EFObjectSpace.ObjectContext property.
    To show objects returned by this method, use a NonPersistentObjectSpace, as described in the How to: Display a Non-Persistent Object's List View from the Navigation topic. Here is an example:

    C#
    [DomainComponent] public class ContactFromSproc { public string FirstName { get; set; } public string LastName { get; set; } } public class ViewController1 : ViewController { public ViewController1() { PopupWindowShowAction showDataFromSprocAction = new PopupWindowShowAction(this, "ShowDataFromSproc", DevExpress.Persistent.Base.PredefinedCategory.View); showDataFromSprocAction.CustomizePopupWindowParams += showDataFromSprocAction_CustomizePopupWindowParams; } void showDataFromSprocAction_CustomizePopupWindowParams(object sender, CustomizePopupWindowParamsEventArgs e) { NonPersistentObjectSpace nonPersistentObjectSpace = (NonPersistentObjectSpace)Application.CreateObjectSpace(typeof(ContactFromSproc)); nonPersistentObjectSpace.ObjectsGetting += nonPersistentObjectSpace_ObjectsGetting; e.View = Application.CreateListView(nonPersistentObjectSpace, typeof(ContactFromSproc), true); } void nonPersistentObjectSpace_ObjectsGetting(object sender, ObjectsGettingEventArgs e) { EFObjectSpace persistentObjectSpace = null; bool disposePersistentObjectSpace = false; if (this.ObjectSpace is EFObjectSpace) { persistentObjectSpace = (EFObjectSpace)ObjectSpace; } else { persistentObjectSpace = (EFObjectSpace)Application.CreateObjectSpace(typeof(Contact)); disposePersistentObjectSpace = true; } e.Objects = persistentObjectSpace.ObjectContext.ExecuteStoreQuery<ContactFromSproc>("GetContacts").ToList(); if (disposePersistentObjectSpace) { persistentObjectSpace.Dispose(); } } } public partial class EFDemoWinApplication : WinApplication { protected override void CreateDefaultObjectSpaceProvider(CreateCustomObjectSpaceProviderEventArgs args) { if(args.Connection != null) { args.ObjectSpaceProvider = new EFObjectSpaceProvider(typeof(EFDemoDbContext), TypesInfo, null, (DbConnection)args.Connection); } else { args.ObjectSpaceProvider = new EFObjectSpaceProvider(typeof(EFDemoDbContext), TypesInfo, null, args.ConnectionString); } args.ObjectSpaceProviders.Add(new NonPersistentObjectSpaceProvider()); }

    Let me know if you need further assistance.

      Show previous comments (12)
      Anatol (DevExpress) 9 years ago

        Despite being 9 years old, this code is still actual, and you can safely use it in your applications. Here is the modified example:

        C#
        public class ViewController1 : ViewController { public ViewController1() { SimpleAction showDataFromSprocAction = new SimpleAction(this, "ShowDataFromSproc", DevExpress.Persistent.Base.PredefinedCategory.View); showDataFromSprocAction.Execute += showDataFromSprocAction_Execute; } void showDataFromSprocAction_Execute(object sender, SimpleActionExecuteEventArgs e) { NonPersistentObjectSpace nonPersistentObjectSpace = (NonPersistentObjectSpace)Application.CreateObjectSpace(typeof(ContactFromSproc)); nonPersistentObjectSpace.ObjectsGetting += nonPersistentObjectSpace_ObjectsGetting; e.ShowViewParameters.CreatedView = Application.CreateListView(nonPersistentObjectSpace, typeof(ContactFromSproc), true); } void nonPersistentObjectSpace_ObjectsGetting(object sender, ObjectsGettingEventArgs e) { EFObjectSpace persistentObjectSpace = (EFObjectSpace)Application.CreateObjectSpace(typeof(Contact)); e.Objects = persistentObjectSpace.ObjectContext.ExecuteStoreQuery<ContactFromSproc>("GetContacts").ToList(); persistentObjectSpace.Dispose(); } }

          Thank's that worked…

          Anatol (DevExpress) 9 years ago

            You are welcome!

            Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

            Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.