protected void btnImport_Click(object sender, EventArgs e)
        {
            var errors = new List <string>();

            txtInfo.Text = "";

            try
            {
                if (!Uri.IsWellFormedUriString(tbxUrl.Text, UriKind.Absolute))
                {
                    errors.Add("Mapping File Url is not a valid Url!");
                }
                if (!Uri.IsWellFormedUriString(tbxRadioLogosBaseUrl.Text, UriKind.Absolute))
                {
                    errors.Add("Radio Logos Base Url is not a valid Url!");
                }
                if (!Uri.IsWellFormedUriString(tbxTVLogosBaseUrl.Text, UriKind.Absolute))
                {
                    errors.Add("TV Logos Base Url is not a valid Url!");
                }
                if (!tbxUrl.Text.ToLower().EndsWith(".xml"))
                {
                    errors.Add("Mapping File Url must point to a xml file!");
                }

                var mappings = new XmlDocument();
                mappings.Load(tbxUrl.Text);

                var xmlTvChannels    = new List <Tuple <Dictionary <string, HashSet <string> >, string, EF.Logo> >();
                var xmlRadioChannels = new List <Tuple <Dictionary <string, HashSet <string> >, string, EF.Logo> >();

                using (var ctx = new EF.RepositoryContext("LogoDB"))
                {
                    var tvChannels = mappings.SelectSingleNode("Mappings/TV");
                    foreach (XmlElement tvChannel in tvChannels.SelectNodes("Channel"))
                    {
                        var xmlTvChannel = GetChannel(tvChannel, 0, ctx, ref errors);
                        if (xmlTvChannel != null)
                        {
                            xmlTvChannels.Add(xmlTvChannel);
                        }
                    }

                    var radioChannels = mappings.SelectSingleNode("Mappings/Radio");
                    foreach (XmlElement radioChannel in radioChannels.SelectNodes("Channel"))
                    {
                        var xmlRadioChannel = GetChannel(radioChannel, 1, ctx, ref errors);
                        if (xmlRadioChannel != null)
                        {
                            xmlRadioChannels.Add(xmlRadioChannel);
                        }
                    }

                    EF.User currentUser = null;
                    var     membership  = System.Web.Security.Membership.GetUser();
                    if (membership != null)
                    {
                        currentUser = ctx.Users.FirstOrDefault(u => u.Id == (Guid)membership.ProviderUserKey);
                    }

                    var repo = ctx.Repositorys.FirstOrDefault();

                    var allProviders = ctx.Providers.ToDictionary(p => p.Name, p => p);

                    foreach (var importChannel in xmlTvChannels)
                    {
                        CreateDbChannel(importChannel, ChannelType.Tv, ctx, repo, currentUser, allProviders);
                    }
                    foreach (var importChannel in xmlRadioChannels)
                    {
                        CreateDbChannel(importChannel, ChannelType.Radio, ctx, repo, currentUser, allProviders);
                    }

                    ctx.ChangeTracker.DetectChanges();
                    ctx.SaveChanges();
                }

                txtInfo.Text = string.Format("Imported {0} TV and {1} Radio Channels.", xmlTvChannels.Count, xmlRadioChannels.Count);
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
            }
            listErrors.DataSource = errors;
            listErrors.DataBind();
            listErrors.Visible = errors.Count > 0;
        }
        void CreateDbChannel(Tuple <Dictionary <string, HashSet <string> >, string, EF.Logo> importChannel, ChannelType channelType, EF.RepositoryContext ctx, EF.Repository repo, EF.User user, Dictionary <string, EF.Provider> knownProviders)
        {
            var channel = ctx.Channels.Create();

            channel.Id         = Guid.NewGuid();
            channel.Type       = channelType;
            channel.Name       = importChannel.Item1.First().Key;
            channel.RegionCode = ddlChannelRegion.SelectedValue;
            repo.Channels.Add(channel);

            var logo = importChannel.Item3;

            logo.Creator = user;
            repo.Logos.Add(logo);
            channel.Logos.Add(logo);
            logo.Channels.Add(channel);

            foreach (var newAlias in importChannel.Item1)
            {
                var alias = ctx.Aliases.Create();
                alias.Created = DateTime.Now;
                alias.Id      = Guid.NewGuid();
                alias.Name    = newAlias.Key;
                channel.Aliases.Add(alias);
                foreach (var aProvider in newAlias.Value)
                {
                    EF.Provider provider = null;
                    if (!knownProviders.TryGetValue(aProvider, out provider))
                    {
                        provider      = ctx.Providers.Create();
                        provider.Id   = Guid.NewGuid();
                        provider.Name = aProvider;
                        repo.Providers.Add(provider);
                        knownProviders.Add(aProvider, provider);
                    }
                    alias.Providers.Add(provider);
                    provider.Aliases.Add(alias);
                }
            }
        }