private bool Save() { bool result = false; using (var ctx = new EF6.RT2020Entities()) { var country = ctx.Country.Find(this.CountryId); if (country == null) { country = new EF6.Country(); country.CountryId = new Guid(); ctx.Country.Add(country); country.CountryCode = txtCountryCode.Text; } country.CountryName = txtCountryName.Text; country.CountryName_Chs = txtCountryNameAlt1.Text; country.CountryName_Cht = txtCountryNameAlt2.Text; ctx.SaveChanges(); result = true; } return(result); }
private static string GetFormatedText(EF6.Country target, string[] textField, string textFormatString) { for (int i = 0; i < textField.Length; i++) { PropertyInfo pi = target.GetType().GetProperty(textField[i]); textFormatString = textFormatString.Replace("{" + i.ToString() + "}", pi != null ? pi.GetValue(target, null).ToString() : string.Empty); } return(textFormatString); }
/// <summary> /// Get a EF6.Country object from the database using the given CountryId /// </summary> /// <param name="countryId">The primary key value</param> /// <returns>A EF6.Country object</returns> public static EF6.Country Get(Guid countryId) { EF6.Country result = null; using (var ctx = new EF6.RT2020Entities()) { result = ctx.Country.Where(x => x.CountryId == countryId).AsNoTracking().FirstOrDefault(); } return(result); }
/// <summary> /// Get a EF6.Country object from the database using the given QueryString /// </summary> /// <param name="countryId">The primary key value</param> /// <returns>A EF6.Country object</returns> public static EF6.Country Get(string whereClause) { EF6.Country result = null; using (var ctx = new EF6.RT2020Entities()) { result = ctx.Country .SqlQuery(string.Format("Select * from Country Where {0}", whereClause)) .AsNoTracking() .FirstOrDefault(); } return(result); }
/// <summary> /// 寫啲 default values 入: /// dbo.Province = Region; dbo.City = District /// </summary> /// <returns></returns> public static bool WriteDefaultValues() { bool result = false; using (var ctx = new EF6.RT2020Entities()) { ctx.Configuration.LazyLoadingEnabled = false; using (var scope = ctx.Database.BeginTransaction()) { try { #region dbo.Country var country = ctx.Country.Where(x => x.CountryCode == "MO").FirstOrDefault(); if (country == null) { country = new EF6.Country(); country.CountryId = Guid.NewGuid(); ctx.Country.Add(country); } country.CountryCode = Macao.Id; country.CountryName = Macao.Name; country.CountryName_Chs = Macao.NameAlt1; country.CountryName_Cht = Macao.NameAlt2; ctx.SaveChanges(); #endregion #region dbo.Province foreach (var item in Region) { var code = String.Format("{0}.{1}", country.CountryCode, item.Id); var province = ctx.Province.Where(x => x.ProvinceCode == code).FirstOrDefault(); if (province == null) { province = new EF6.Province() { ProvinceId = Guid.NewGuid(), ProvinceCode = String.Format("{0}.{1}", country.CountryCode, item.Id) }; ctx.Province.Add(province); } province.ProvinceName = item.Name; province.ProvinceName_Chs = item.NameAlt1; province.ProvinceName_Cht = item.NameAlt2; province.CountryId = country.CountryId; } ctx.SaveChanges(); #endregion /** 澳門冇分 Districts #region cbo.City * foreach (var item in District) * { * var parent = String.Format("{0}.{1}", country.CountryCode, item.Parent); * var province = ctx.Province.Where(x => x.ProvinceCode == parent).FirstOrDefault(); * if (province != null) * { * var code = String.Format("{0}.{1}", province.ProvinceCode, item.Id); * var city = ctx.City.Where(x => x.CityCode == code).FirstOrDefault(); * if (city == null) * { * city = new EF6.City() * { * CityId = Guid.NewGuid(), * CityCode = String.Format("{0}.{1}", province.ProvinceCode, item.Id), * }; * ctx.City.Add(city); * } * city.CityName = item.Name; * city.CityName_Chs = item.NameAlt1; * city.CityName_Cht = item.NameAlt2; * city.ProvinceId = province.ProvinceId; * } * } * ctx.SaveChanges(); #endregion */ scope.Commit(); result = true; } catch { scope.Rollback(); } } } return(result); }