Click here to Skip to main content
15,896,469 members
Articles / Database Development / SQL Server

Localization with Entity Framework

Rate me:
Please Sign up or sign in to vote.
4.43/5 (5 votes)
19 Apr 2012CPOL2 min read 31.5K   578   15  
How to replace textual properties of EF Objects with translations
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;

namespace EF_Translate.Data
{
    public partial class Language : IEntity
    
    {
        #region Primitive Properties
    
        public virtual int Id
        {
            get;
            set;
        }
    
        public virtual string Code
        {
            get;
            set;
        }
    
        public virtual string Name
        {
            get;
            set;
        }

        #endregion
        #region Navigation Properties
    
        public virtual ICollection<LocalizableEntityTranslation> LocalizableEntityTranslation
        {
            get
            {
                if (_localizableEntityTranslation == null)
                {
                    var newCollection = new FixupCollection<LocalizableEntityTranslation>();
                    newCollection.CollectionChanged += FixupLocalizableEntityTranslation;
                    _localizableEntityTranslation = newCollection;
                }
                return _localizableEntityTranslation;
            }
            set
            {
                if (!ReferenceEquals(_localizableEntityTranslation, value))
                {
                    var previousValue = _localizableEntityTranslation as FixupCollection<LocalizableEntityTranslation>;
                    if (previousValue != null)
                    {
                        previousValue.CollectionChanged -= FixupLocalizableEntityTranslation;
                    }
                    _localizableEntityTranslation = value;
                    var newValue = value as FixupCollection<LocalizableEntityTranslation>;
                    if (newValue != null)
                    {
                        newValue.CollectionChanged += FixupLocalizableEntityTranslation;
                    }
                }
            }
        }
        private ICollection<LocalizableEntityTranslation> _localizableEntityTranslation;

        #endregion
        #region Association Fixup
    
        private void FixupLocalizableEntityTranslation(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null)
            {
                foreach (LocalizableEntityTranslation item in e.NewItems)
                {
                    item.Language = this;
                }
            }
    
            if (e.OldItems != null)
            {
                foreach (LocalizableEntityTranslation item in e.OldItems)
                {
                    if (ReferenceEquals(item.Language, this))
                    {
                        item.Language = null;
                    }
                }
            }
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Unknown
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions