none
Entity Framework DataBase first Property Changed Event RRS feed

  • Question

  • hi,
    i have moved forward to try entity framework,but i have problem.

    in ado.net DataTables we could have the option to Subscribe to ColumnChanged Event and if a column change to raise an event and do some things .

    Now by using entity framework DataBase First when i bind either of the following ObservableCollection, Collectionview, CollectionViewSource  i could not find any way to notify if any of the property changed.

    only CollectionChanged event is available and CurrentChanged. 
    I would like to be notified when the user in the UI change a property to perform some other calculations to the other columns  - properties . How to do that?

    my Entities are not created by me (Entities are created by an existing SQL Database ) so i cannot implement InotifyPropertyChanged to my class.

    i am using Vb.Net 

    any help is appreciated. 



    stelios ----------


    • Edited by stelios84 Sunday, November 1, 2015 11:48 AM
    Saturday, October 31, 2015 3:41 PM

Answers

  • only CollectionChanged event is available and CurrentChanged. 
    I would like to be notified when the user in the UI change a property to perform some other calculations to the other columns  - properties . How to do that?

    Well, you don't use the EF entities at the UI. In the example of using EF in the Data Access Layer with a WPF MVVM solution, a separation of concerns, the EF entitles are left at the DAL and DTO(s) are sent through the n-tiers. The ViewModel is what is connected to the UI and not the EF entities.

    https://en.wikipedia.org/wiki/Separation_of_concerns

    So, you need to be making custom objects for the UI with a Setter that will implement PropertyChange().

    Or you can make custom objects that set an object's dirty state based on a property changed like in the link.

    In the link is another way of making a custom object that can be used at the UI so when a user interacts with a control that is tied to a custom object's property, the object's dirty state is raised notifying that the object has changed. Forget about it is talking about business objects which are just custom objects.

    http://www.dailycoding.com/Posts/maintaining_dirty_and_new_state_of_objects.aspx

    Of course, there is mapping of data between the custom object and the EF entity.

    using System.Linq;
    using System.Windows.Input;
    using DemoWPFApp.Properties;
    using Services.IServices;
    using System.Collections.Generic;
    using BLL.DTO;
    
    namespace DemoWPFApp.ViewModel
    {
        public class ArticleViewModel : WorkspaceViewModel
        {
    
            private readonly IService1 mService;
            private int idx;
    
            private string _articleid;
            private string _title;
            private string _body;
            private DTOArticle _dtoarticle;
            private List<DTOArticle> _articles;
            private RelayCommand _updateCommand;
            private RelayCommand _addCommand;
            private RelayCommand _saveCommand;
            private RelayCommand _deleteCommand;
            private RelayCommand _moveLastCommand;
            private RelayCommand _movePrevCommand;
            private RelayCommand _moveNextCommand;
            private RelayCommand _moveFirstCommand;
            private RelayCommand _clearCommand;
    
            public ArticleViewModel(IService1 theService)
            {
                mService = theService;
    
                _articles = mService.GetArticles();
                _dtoarticle = mService.GetArticleTop1();
                idx = 0;
                PopulateProperties(_dtoarticle);
    
                base.DisplayName = Strings.ArticleViewModel_DisplayName;
            }
    
            public string ArticleID
            {
                get { return _articleid; }
                set
                {
                    _articleid = value;
                    base.OnPropertyChanged("ArticleID");
                }
            }
    
            public string Title
            {
                get { return _title; }
                set
                {
                    _title = value;
                    base.OnPropertyChanged("Title");
                }
            }
    
            public string Body
            {
                get
                {
                    return _body;
                }
                set
                {
                    _body = value;
                    base.OnPropertyChanged("Body");
                }
            }
    
            public override string DisplayName
            {
                get
                {
                    {
                        return Strings.ArticleViewModel_DisplayName;
                    }
                }
            }
    
            public ICommand UpdateCommand
            {
                get
                {
                    if (_updateCommand == null)
                    {
                        _updateCommand = new RelayCommand(
                            param => Update(),
                            param => CanUpdate
                            );
                    }
                    return _updateCommand;
                }
            }
    
            public ICommand AddCommand
            {
                get
                {
                    if (_addCommand == null)
                    {
                        _addCommand = new RelayCommand(
                            param => Add(),
                            param => CanAdd
                            );
                    }
                    return _addCommand;
                }
            }
    
            public ICommand SaveCommand
            {
                get
                {
                    if (_saveCommand == null)
                    {
                        _saveCommand = new RelayCommand(
                            param => Save(),
                            param => CanSave
                            );
                    }
                    return _saveCommand;
                }
            }
    
            public ICommand DeleteCommand
            {
                get
                {
                    if (_deleteCommand == null)
                    {
                        _deleteCommand = new RelayCommand(
                            param => Delete(),
                            param => CanDelete
                            );
                    }
                    return _deleteCommand;
                }
            }
    
            public ICommand ClearCommand
            {
                get
                {
                    if (_clearCommand == null)
                    {
                        _clearCommand = new RelayCommand(
                            param => Clear(),
                            param => CanClear
                            );
                    }
                    return _clearCommand;
                }
            }
    
            public ICommand MoveLastCommand
            {
                get
                {
                    if (_moveLastCommand == null)
                    {
                        _moveLastCommand = new RelayCommand(
                            param => MoveLast(),
                            param => CanMove
                            );
                    }
                    return _moveLastCommand;
                }
            }
    
            public ICommand MoveFirstCommand
            {
                get
                {
                    if (_moveFirstCommand == null)
                    {
                        _moveFirstCommand = new RelayCommand(
                            param => MoveFirst(),
                            param => CanMove
                            );
                    }
                    return _moveFirstCommand;
                }
            }
    
            public ICommand MovePrevCommand
            {
                get
                {
                    if (_movePrevCommand == null)
                    {
                        _movePrevCommand = new RelayCommand(
                            param => MovePrev(),
                            param => CanMove
                            );
                    }
                    return _movePrevCommand;
                }
            }
    
            public ICommand MoveNextCommand
            {
                get
                {
                    if (_moveNextCommand == null)
                    {
                        _moveNextCommand = new RelayCommand(
                            param => MoveNext(),
                            param => CanMove
                            );
                    }
                    return _moveNextCommand;
                }
            }
    
            private void PopulateProperties(DTOArticle article)
            {
                ArticleID = article.ArticleID.ToString();
                Title = article.Title;
                Body = article.Body;
            }
    
            private void PopulateProperties(List<DTOArticle> articles, int idxx)
            {
                ArticleID = articles[idxx].ArticleID.ToString();
                Title = articles[idxx].Title;
                Body = articles[idxx].Body;
            }
    
            private void Update()
            {
    
                if(_articles != null)
                {
                    _articles[idx].Title = Title;
                    _articles[idx].Body = Body;
                    _articles[idx].IsUpdate = true;
                }
    
                base.OnPropertyChanged("DisplayName");
            }
    
            private void Add()
            {
    
                if (_articles != null)
                {
                    if (Title != string.Empty
                    && Body != string.Empty
                    && ArticleID == "Article not saved yet.")
                    {
                        var dto = new DTOArticle  {ArticleID = -1,   AuthorID = 1, Title = Title, Body = Body, IsUpdate = false};
    
                        _articles.Add(dto);
                        idx = _articles.Count - 1;
                    }
                }
    
                base.OnPropertyChanged("DisplayName");
            }
    
            private void Save()
            {
    
                if (_articles != null)
                {
                    var atleastonearticle = (from a in _articles.Where(a => a.IsUpdate
                                         || a.ArticleID == -1)
                                            select a).FirstOrDefault();
    
                    if (atleastonearticle != null)
                    {
                        _articles = mService.SaveArticles(_articles);
                        PopulateProperties(_articles, idx);
                    }
                }
    
                base.OnPropertyChanged("DisplayName");
            }
    
            private void Delete()
            {
                if (_articles.Count > 0)
                {
                    var article = _articles[idx];
                    mService.DeleteArticle(article);
                    _articles.RemoveAt(idx);
                    idx = 0;
                    PopulateProperties(_articles, idx);
                }
            }
    
            private void MoveLast()
            {
    
                if (_articles != null)
                {
                    idx = _articles.Count - 1;
                    PopulateProperties(_articles, idx);
                }
    
                base.OnPropertyChanged("DisplayName");
            }
    
            private void MovePrev()
            {
    
                if (_articles != null)
                {
                    if (idx == 0)
                    {
                        PopulateProperties(_articles, idx);
                    }
                    else
                    {
                        idx -= 1;
                        PopulateProperties(_articles, idx);
                    }
                }
    
                base.OnPropertyChanged("DisplayName");
            }
    
            private void MoveNext()
            {
    
                if (_articles != null)
                {
                    if (idx == _articles.Count - 1)
                    {
                        PopulateProperties(_articles, idx);
                    }
                    else
                    {
                        idx += 1;
                        PopulateProperties(_articles, idx);
                    }
                }
    
                base.OnPropertyChanged("DisplayName");
            }
    
            private void MoveFirst()
            {
    
                if (_articles != null)
                {
                    idx = 0;
                    PopulateProperties(_articles, idx);
                }
    
                base.OnPropertyChanged("DisplayName");
            }
    
            private void Clear()
            {
                Title = "";
                Body = "";
                ArticleID = "Article not saved yet.";
            }
    
            bool CanUpdate
            {
                get { return true; }
            }
    
            bool CanAdd
            {
                get { return true; }
            }
    
            bool CanDelete
            {
                get { return true; }
            }
    
            bool CanSave
            {
                get { return true; }
            }
    
            bool CanMove
            {
                get { return true; }
            }
    
            bool CanClear
            {
                get { return true; }
            }
        }
    }
     

    Sunday, November 1, 2015 5:36 AM