//Method that creates new Company public bool CreateNewCompany(CompanyDTO CompanyNew) { bool check = false; using (TransactionScope transaction = new TransactionScope()) { try { Company CompanyIn = new Company(); //Data transfer to the new company CompanyIn.Name = CompanyNew.Name; using (var context = new RFQEntities()) { //Finding references for the Foreign Keys Category existingCategory = context.Categories.Single(p => p.CategoryID == CompanyNew.CategoryID); CompanyType existingCompanyType = context.CompanyTypes.Single(p => p.CompanyTypeID == CompanyNew.CompanyTypeID); //Adding refernces CompanyIn.Category = existingCategory; CompanyIn.CompanyType = existingCompanyType; if (CompanyIn.EntityState == EntityState.Detached) { context.Companies.AddObject(CompanyIn); } context.SaveChanges(); } } catch (Exception e) { transaction.Dispose(); check = false; return check; } transaction.Complete(); check = true; return check; } }
/// <summary> /// Create a new Company object. /// </summary> /// <param name="companyID">Initial value of the CompanyID property.</param> /// <param name="name">Initial value of the Name property.</param> /// <param name="companyTypeID">Initial value of the CompanyTypeID property.</param> /// <param name="categoryID">Initial value of the CategoryID property.</param> /// <param name="phone">Initial value of the Phone property.</param> /// <param name="email">Initial value of the Email property.</param> public static Company CreateCompany(global::System.Int32 companyID, global::System.String name, global::System.Int32 companyTypeID, global::System.Int32 categoryID, global::System.String phone, global::System.String email) { Company company = new Company(); company.CompanyID = companyID; company.Name = name; company.CompanyTypeID = companyTypeID; company.CategoryID = categoryID; company.Phone = phone; company.Email = email; return company; }
//Get Company By ID Method public CompanyDTO GetCompanyByID(int CompanyID) { Company CompanySpecific = new Company(); string path = "Category"; string path1 = "CompanyType"; using (var context = new RFQEntities()) { CompanySpecific = (from comp in context.Companies.Include(path).Include(path1) where comp.CompanyID == CompanyID select comp).First(); } CompanyDTO CompanyToReturn = new CompanyDTO(); CompanyToReturn.CompanyID = CompanySpecific.CompanyID; CompanyToReturn.Name = CompanySpecific.Name; CompanyToReturn.CategoryID = CompanySpecific.Category.CategoryID; CompanyToReturn.CompanyTypeID = CompanySpecific.CompanyType.CompanyTypeID; return CompanyToReturn; }
/// <summary> /// Deprecated Method for adding a new object to the Companies EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToCompanies(Company company) { base.AddObject("Companies", company); }