none
DataForm CRUD Operations with RIA Domain Service

    Question

  • Hello,

    I have a database model, and I've created an EDMX scheme to access the entities.

    In order to access the data model from silverlight client, I've created a domain service, and selected entities, with editing enabled, and associated classes for metadata are generated, and decorated as needed.

    I have 10 associative entities, like EmployeeCitizenship, EmployeePosiiton, that built to hold information for table employee.

    I'm creating the CRUD interface for all associative entities, so I placed a tab control, and according to active tab item, the appropriate server query will be executed.

    Once the IEnumerable<Entity> populated from server side, the result property in ModelView will be updated, and a DataForm control on the view side read these changes, because its two way binding between this property and the DataForm control.

    The data form, navigate to different items, and I can update record, by calling DataForm.CommitEdit(); and then check the DataContext for changes, and finally call SubmitChanges.

    The Add and Delete icons are grayed, and I can't add new record or delete item, but I can edit! What exactly I'm missing to get the add and delete buttons enabled?

     

    Thanks

    Wednesday, April 11, 2012 3:41 PM

Answers

  • Hi,

     

    I found what was wrong with my code, the following code should not use AllEntities, instead; I have to use cb.Entities.ToList();

    AM.Load(AM.GetEquipmentStatusByOperationQuery(SelectedOperation), cb => { AssociativeList = cb.AllEntities; }, null);

     

    Sunday, April 15, 2012 3:13 PM

All replies

  • Hi FrostedCore,

    The Add and Delete icons are grayed, and I can't add new record or delete item, but I can edit! What exactly I'm missing to get the add and delete buttons enabled?

    Based on your description above, I can't reproduce your issue. Could you simply your project and upload to skydrive? Then give a link. We want to test it locally.

    Thank you for your support!

    Friday, April 13, 2012 2:20 AM
  • Hi Catherine,

    The below code for the metadata class

        [MetadataTypeAttribute(typeof(EmployeeCitizenship.EmployeeCitizenshipMetadata))]
        public partial class EmployeeCitizenship
        {
            internal sealed class EmployeeCitizenshipMetadata
            {
     
                // Metadata classes are not meant to be instantiated.
                private EmployeeCitizenshipMetadata()
                {
                }
     
                [Display(Name = "Citizenship", Description = "Employee Citizenship")]
                public string Citizenship { getset; }
     
                [Display(AutoGenerateField = false)]
                public int CitizenshipID { getset; }
     
                [Display(AutoGenerateField = false)]
                public EntityCollection<Employee> Employees { getset; }
     
                [Display(Name = "Is Enabled")]
                public bool Enabled { getset; }
     
                [Display(AutoGenerateField = false)]
                public Operation Operation { getset; }
     
                [Display(AutoGenerateField = false)]
                public int OperationID { getset; }
     
                [Display(AutoGenerateField = false)]
                public DateTime TimeStamp { getset; }
            }
        }
    

     The Domain Service code:

       
        [EnableClientAccess()]
        public class AssociativeManager : LinqToEntitiesDomainService<ITESPOCEntities>
        {
     
            // TODO:
            // Consider constraining the results of your query method.  If you need additional input you can
            // add parameters to this method or create additional query methods with different names.
            // To support paging you will need to add ordering to the 'EmployeeCitizenships' query.
            public IQueryable<EmployeeCitizenship> GetEmployeeCitizenships()
            {
                return this.ObjectContext.EmployeeCitizenships;
            }
     
            public IQueryable<EmployeeCitizenship> GetEmployeeCitizenshipsByOperation(int OperationID)
            {
                return this.ObjectContext.EmployeeCitizenships.Where(p => p.OperationID == OperationID);
            }
     
            public void InsertEmployeeCitizenship(EmployeeCitizenship employeeCitizenship)
            {
                if ((employeeCitizenship.EntityState != EntityState.Detached))
                {
                    this.ObjectContext.ObjectStateManager.ChangeObjectState(employeeCitizenship, EntityState.Added);
                }
                else
                {
                    this.ObjectContext.EmployeeCitizenships.AddObject(employeeCitizenship);
                }
            }
     
            public void UpdateEmployeeCitizenship(EmployeeCitizenship currentEmployeeCitizenship)
            {
                this.ObjectContext.EmployeeCitizenships.AttachAsModified(currentEmployeeCitizenship, this.ChangeSet.GetOriginal(currentEmployeeCitizenship));
            }
     
            public void DeleteEmployeeCitizenship(EmployeeCitizenship employeeCitizenship)
            {
                if ((employeeCitizenship.EntityState != EntityState.Detached))
                {
                    this.ObjectContext.ObjectStateManager.ChangeObjectState(employeeCitizenship, EntityState.Deleted);
                }
                else
                {
                    this.ObjectContext.EmployeeCitizenships.Attach(employeeCitizenship);
                    this.ObjectContext.EmployeeCitizenships.DeleteObject(employeeCitizenship);
                }
            }
        }
    

     View Model

    namespace Amplexus.ViewModel
    {
        public class ManageAssociativeModel : INotifyPropertyChanged
        {
            #region > Variables <
                public AssociativeManager AM;
               // public IEnumerable<Entity> AssociativeList;
            #endregion
     
            #region > Properties <
                private List<Entity> _AssociativeList;
                public List<Entity> AssociativeList
                {
                    get
                    {
                        return _AssociativeList;
                    }
                    set
                    {
                        _AssociativeList = value;
                        NotifyPropertyChanged("AssociativeList");
                    }
                }
     
                private IEnumerable<Operation> _Operations;
                public IEnumerable<Operation> Operations
                {
                    get
                    {
                        return _Operations;
                    }
                    set
                    {
                        _Operations = value;
                        if (_Operations != null && _Operations.Count() > 0)
                        {
                            SelectedOperation = _Operations.FirstOrDefault().OperationID;
                        }
     
                        NotifyPropertyChanged("Operations");
                    }
                }
     
                private int _SelectedOperation;
                public int SelectedOperation 
                {
                    get
                    {
                        return _SelectedOperation;
                    }
                    set
                    {
                        _SelectedOperation = value;
                        NotifyPropertyChanged("SelectedOperation");
                    }
                }
     
                private bool _IsBusyLoadingOperation;
                public bool IsBusyLoadingOperation
                {
                    get
                    {
                        return _IsBusyLoadingOperation;
                    }
                    set
                    {
                        _IsBusyLoadingOperation = value;
                        NotifyPropertyChanged("IsBusyLoadingOperation");
                    }
                }
     
                private string _SelectedAssociativeCategory;
                public string SelectedAssociativeCategory {
                    get
                    {
                        return _SelectedAssociativeCategory;
                    }
                    set
                    {
                        _SelectedAssociativeCategory = value;
                        NotifyPropertyChanged("SelectedAssociativeCategory");
                    }
                }
     
                private string _SelectedAssociative;
                public string SelectedAssociative
                {
                    get
                    {
                        return _SelectedAssociative;
                    }
                    set
                    {
                        _SelectedAssociative = value;
                        NotifyPropertyChanged("SelectedAssociative");
                        LoadAssociative(SelectedAssociative);
                    }
                }
            #endregion
     
            #region > Constructors <
                public ManageAssociativeModel()
                {
                    AM = new AssociativeManager();
                    LoadOperationsList();
                }
            #endregion
     
            #region > Loaders <
                public void LoadOperationsList()
                {
                    IsBusyLoadingOperation = true;
                    AM.Load(AM.GetOperationsQuery(),
                        cb =>
                        {
                            Operations = cb.Entities;
                            IsBusyLoadingOperation = false;
                        }, null);
                }
                public void LoadAssociative(string AssociativeName)
                {
                    switch (AssociativeName)
                    {
                        case ("CITIZENSHIP"):
                            AM.Load(AM.GetEmployeeCitizenshipsByOperationQuery(SelectedOperation), 
                            cb => 
                            {
                                AssociativeList = cb.Entities.ToList<Entity>(); 
                            } , null);
                            break;
                        case ("POSITION"):
                            AM.Load(AM.GetEmployeePositionsByOperationQuery(SelectedOperation), cb => { AssociativeList = cb.Entities.ToList<Entity>(); }, null);
                            break;
                        case ("STATUS CODE"):
                            AM.Load(AM.GetEmployeeStatusCodesByOperationQuery(SelectedOperation), cb => { AssociativeList = cb.Entities.ToList<Entity>(); }, null);
                            break;
                        case ("TYPE"):
                            AM.Load(AM.GetEquipmentTypesByOperationQuery(SelectedOperation), cb => { AssociativeList = cb.Entities.ToList<Entity>(); }, null);
                            break;
                        case ("BRAND"):
                            AM.Load(AM.GetEquipmentBrandsByOperationQuery(SelectedOperation), cb => { AssociativeList = cb.Entities.ToList<Entity>(); }, null);
                            break;
                        case ("DESIGNATION"):
                            AM.Load(AM.GetEquipmentDesignationsByOperationQuery(SelectedOperation), cb => { AssociativeList = cb.Entities.ToList<Entity>(); }, null);
                            break;
                        case ("STATUS"):
                            AM.Load(AM.GetEquipmentStatusByOperationQuery(SelectedOperation), cb => { AssociativeList = cb.Entities.ToList<Entity>(); }, null);
                            break;
                        default:
                            break;
                    }
                }
                public void SubmitChanges()
                {
                    if (AM.HasChanges)
                    {
                        AM.SubmitChanges();
                    }
                }
            #endregion
     
            #region > INotifyPropertyChanged >
            public event PropertyChangedEventHandler PropertyChanged;
     
            private void NotifyPropertyChanged(String info)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(thisnew PropertyChangedEventArgs(info));
                }
            }
            #endregion
        }
    }

    View XAML Code:

    <UserControl
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
     xmlns:local="clr-namespace:Amplexus.ViewModel"
     xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
     xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"
     mc:Ignorable="d"
     x:Class="Amplexus.ManageAssociative"
     d:DesignWidth="1024" d:DesignHeight="768">
        <UserControl.Resources>
            <local:ManageAssociativeModel x:Key="AssociativeViewModel" d:IsDataSource="True"/>
        </UserControl.Resources>
     <UserControl.DataContext>
     <Binding Source="{StaticResource AssociativeViewModel}"/>
     </UserControl.DataContext>
     
        <Grid x:Name="LayoutRoot" >
         <toolkit:BusyIndicator x:Name="OperationBusyIndicator" Margin="15,15,30,0" VerticalAlignment="Top" HorizontalAlignment="Left" 
                                   Style="{StaticResource BusyIndicatorLoadingStyle}" 
                                   BusyContent="PLEASE WAIT..." 
                                   Foreground="#FF6E6E6E"
                                   IsBusy="{Binding IsBusyLoadingOperation, Mode=OneWay}">
         <ComboBox x:Name="OperationsList" Width="300" 
        		 ItemsSource="{Binding Operations, Mode=OneWay}"
        		 SelectedValue="{Binding SelectedOperation, Mode=TwoWay}"
        		 DisplayMemberPath="OperationName"
        		 SelectedValuePath="OperationID"/>
         </toolkit:BusyIndicator>
         <Grid Margin="15,59,20,20">
         <Border Margin="205,3,0,0" BorderBrush="{StaticResource PageBorderBrush}" BorderThickness="1,0,0,0" >
         <Grid Margin="0,-9,0,0">
         <Grid.RowDefinitions>
         <RowDefinition Height="695"/>
         </Grid.RowDefinitions>
         <toolkit:DataForm 
        				 x:Name="EmployeeDataForm" 
        				 LabelPosition="Top" 
        				 Header="Associative Manager" 
        				 CommitButtonContent="Save"  
        				 CancelButtonContent="Cancel"
    					 CommandButtonsVisibility="All"
                            AutoCommit="False"
                            AutoEdit="False"
                            
                            ItemsSource="{Binding AssociativeList, Mode=TwoWay}"
     
        				 BorderBrush="{x:Null}" Background="{x:Null}" Margin="0" EditEnded="EmployeeDataForm_EditEnded" AddingNewItem="EmployeeDataForm_AddingNewItem" DeletingItem="EmployeeDataForm_DeletingItem" BeginningEdit="EmployeeDataForm_BeginningEdit"  />
         </Grid>
         </Border>
         <sdk:TabControl TabStripPlacement="Left" BorderBrush="{x:Null}" SelectionChanged="AssociativeCategory_SelectionChanged" HorizontalAlignment="Left">
         <sdk:TabItem Header="EMPLOYEE" Background="{x:Null}" FontSize="10.667" FontFamily="{StaticResource UIContentFontFamily}" Width="74">
         <Border Margin="5,-5,0,-5" BorderBrush="{StaticResource PageBorderBrush}" BorderThickness="1,0,0,0">
         <sdk:TabControl x:Name="EmployeeAssociative" TabStripPlacement="Left" BorderBrush="{x:Null}" SelectionChanged="Associative_SelectionChanged" Margin="10,0,0,0">
         <sdk:TabItem Header="CITIZENSHIP" FontSize="10.667" FontFamily="{StaticResource UIContentFontFamily}"/>
         <sdk:TabItem Header="POSITION" FontSize="10.667" FontFamily="{StaticResource UIContentFontFamily}"/>
         <sdk:TabItem Header="STATUS CODE" Background="{x:Null}" FontSize="10.667" FontFamily="{StaticResource UIContentFontFamily}"/>
         </sdk:TabControl>
         </Border>
         </sdk:TabItem>
         <sdk:TabItem Header="TOOL" FontSize="10.667" FontFamily="{StaticResource UIContentFontFamily}" Width="74">
         <Border Margin="5,-5,0,-5" BorderBrush="{StaticResource PageBorderBrush}" BorderThickness="1,0,0,0">
         <sdk:TabControl x:Name="ToolAssociative" TabStripPlacement="Left" BorderBrush="{x:Null}" SelectionChanged="Associative_SelectionChanged" Margin="10,0,0,0">
         <sdk:TabItem Header="TYPE" Background="{x:Null}" FontSize="10.667" FontFamily="{StaticResource UIContentFontFamily}"/>
         <sdk:TabItem Header="BRAND" FontSize="10.667" FontFamily="{StaticResource UIContentFontFamily}"/>
         <sdk:TabItem Header="DESIGNATION" FontSize="10.667" FontFamily="{StaticResource UIContentFontFamily}"/>
         <sdk:TabItem Header="STATUS" Background="{x:Null}" FontSize="10.667" FontFamily="{StaticResource UIContentFontFamily}"/>
         </sdk:TabControl>
         </Border>
         </sdk:TabItem>
         </sdk:TabControl>
         </Grid>
        </Grid>
    </UserControl>

    The link to see the application, and the edit button enabled: http://210.5.212.4/#/Administration/ManageUsers

    Friday, April 13, 2012 8:15 AM
  • Hi,

     

    I found what was wrong with my code, the following code should not use AllEntities, instead; I have to use cb.Entities.ToList();

    AM.Load(AM.GetEquipmentStatusByOperationQuery(SelectedOperation), cb => { AssociativeList = cb.AllEntities; }, null);

     

    Sunday, April 15, 2012 3:13 PM
  • Hi FrostedCore,

    Very glad to hear that you have found out the issue.

    Have you resolved your issue already? If you have resolved it, please explain it more clearly and mark it the answer.  That will help others to learn.

    Best wishes,

    Monday, April 16, 2012 10:08 AM