我正在开发一个带有实体框架和Csharp的预会计软件。
我得到“System.NullReferenceException:'对象引用未设置为对象的实例。'”尝试时将错误添加到数据库中。
有关错误的实体;
该实体保留客户和供应商的数据。
public class CompanyInfo
{
public int CompanyInfoId { get; set; }
public string Title { get; set; }
public string AddressCity { get; set; }
public string AddressDistrict { get; set; }
public string AddressNeighborhood { get; set; }
public string AddressStreet { get; set; }
public string AddressNo { get; set; }
public string AddressFloor { get; set; }
public string TaxNo { get; set; }
public string TaxAdministration { get; set; }
public string AuthorizedPerson { get; set; }
public bool AuthorizedPersonGender { get; set; }
public string Tel { get; set; }
public string Fax { get; set; }
public string Mail { get; set; }
public string TradeRegistryNo { get; set; }
public DateTime CreateDate { get; set; }
public bool IsActive { get; set; }
public string Iban { get; set; }
public bool IsSupplier { get; set; }
public bool IsCustomer { get; set; }
//Navigation Property
public virtual UserCompany UserCompany{ get; set; }
public virtual ICollection<Invoice> Invoices { get; set; }
public virtual ICollection<PeriodicService> PeriodicServices{ get; set; }
public virtual ICollection<FinancialTransaction> FinancialTransactions { get; set; }
public CompanyInfo()
{
Invoices = new HashSet<Invoice>();
PeriodicServices = new HashSet<PeriodicService>();
FinancialTransactions = new HashSet<FinancialTransaction>();
}
}
此实体保留用户公司的数据。
public class UserCompany
{
public int UserCompanyId { get; set; }
public string Title { get; set; }
public string AdressCity { get; set; }
public string AdressNeighborhood { get; set; }
public string AdressDistrict { get; set; }
public string AdressStreet { get; set; }
public string AdressNo { get; set; }
public string AdressFloor { get; set; }
public string TaxNo { get; set; }
public string TaxAdministration { get; set; }
public string LogoPath { get; set; }
public string Tel { get; set; }
public string Fax { get; set; }
public string Mail { get; set; }
public string TradeRegistryNo { get; set; }
public DateTime CreateDate { get; set; }
public bool IsActive { get; set; }
public string Iban { get; set; }
// Navigation Property
public virtual ICollection<ProgramUser> ProgramUsers { get; set; }
public virtual ICollection<CompanyInfo> CompanyInfos { get; set; }
public virtual ICollection<PeriodicService> PeriodicServices { get; set; }
public virtual ICollection<FinancialTransaction> FinancialTransactions { get; set; }
public virtual ICollection<Invoice> Invoices { get; set; }
public virtual ICollection<Service> Services { get; set; }
public virtual ICollection<Stock> Products { get; set; }
public UserCompany()
{
ProgramUsers = new HashSet<ProgramUser>();
CompanyInfos = new HashSet<CompanyInfo>();
PeriodicServices = new HashSet<PeriodicService>();
FinancialTransactions = new HashSet<FinancialTransaction>();
Invoices = new HashSet<Invoice>();
Services = new HashSet<Service>();
Products = new HashSet<Stock>();
}
}
因此,UserCompany和CompanyInfo之间存在一对多的关系。
此代码用于添加公司并与用户公司建立关系......
public void AddCompany()
{
if (CheckRequiredAddCompanyFields())
{
CompanyInfo newCompany = new CompanyInfo();
newCompany.Title = addCompanyForm.mtxtDetailTitle.Text;
newCompany.AddressCity = addCompanyForm.mtxtCity.Text;
newCompany.AddressDistrict = addCompanyForm.mtxtDistrict.Text;
newCompany.AddressNeighborhood = addCompanyForm.mtxtNeighborhood.Text;
newCompany.AddressStreet = addCompanyForm.mtxtStreet.Text;
newCompany.AddressNo = addCompanyForm.mtxtAdressNo.Text;
newCompany.AddressFloor = addCompanyForm.mtxtFloor.Text;
newCompany.TaxNo = addCompanyForm.mtxtTaxNo.Text;
newCompany.TaxAdministration = addCompanyForm.mtxtTaxAdministration.Text;
newCompany.AuthorizedPerson = addCompanyForm.mtxtAuthorizedPerson.Text;
newCompany.AuthorizedPersonGender = addCompanyForm.mrdbtnMale.Checked ? true : false;
newCompany.Tel = addCompanyForm.mtxtDetailTel.Text;
newCompany.Fax = addCompanyForm.mtxtDetailFax.Text;
newCompany.Mail = addCompanyForm.mtxtMail.Text;
newCompany.TradeRegistryNo = addCompanyForm.mtxtTradeRegistryNo.Text;
newCompany.CreateDate = DateTime.Today;
newCompany.IsActive = true;
newCompany.Iban = addCompanyForm.mtxtIban.Text;
newCompany.IsCustomer = addCompanyForm.mcboxDetailCustomer.Checked == true ? true : false;
newCompany.IsSupplier = addCompanyForm.mcboxDetailSupplier.Checked == true ? true: false;
//The error was thrown from the following line
newCompany.UserCompany.UserCompanyId = mainForm.userCompanyId;
bCompanyInfo.Add(newCompany);
FillCompaniesToDataGrid(mainForm.userCompanyId);
}
}