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.
EF - How to show data from a stored procedure in a ListView with Entity Framework 6
Answers approved by DevExpress Support
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):
- How to: Display a List View With Data From a Stored Procedure With a Parameter
- How to: Display a Detail View With Data From a Stored Procedure From the Navigation
- How to: Display a List View With Data From a Stored Procedure From the Navigation
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
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.
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();
}
}