因为我的英文不好,我就直接开门见山了。为什么在数据库中记录公司创建新记录而客户记录引用新公司记录?感谢帮助 :)
public class Company : EntityBase
{
public string Name { get; set; }
public List<Customer> Customers { get; set; }
public List<Invoice> Invoices { get; set; }
}
public class Customer : EntityBase
{
public string Name { get; set; }
public Company Company { get; set; }
public List<Card> Cards { get; set; }
}
public class EFRepositoryBase<TEntity> where TEntity : class, IEntity, new()
{
protected IUnitOfWork UnitOfWork { get; set; }
protected BenzineFleetContext Context
{
get { return (BenzineFleetContext) UnitOfWork; }
}
public virtual DbSet<TEntity> GetDbSet<TEntity>() where TEntity : class
{
return Context.Set<TEntity>();
}
public virtual void Add(TEntity entity)
{
GetDbSet<TEntity>().Add(entity);
}
public virtual void SaveChanges()
{
Context.SaveChanges();
}
}
//Save
var cus = 新客户 {Company = SelectedCompany}
_srv.Add(cus);
_srv.SaveChanges();