在c#和实体框架中,如何确保现有记录不能更改,并且只有在通过检查后才能添加新记录?
我想在c#6和entity framework 6中创建一个简单的总账应用程序,但我想我正在努力进行正确的设计 从我的(简化的)观点来看,总账是一个交易的集合,以后可以用它来编译相关的数据/报告 我对交易的要求如下:在c#和实体框架中,如何确保现有记录不能更改,并且只有在通过检查后才能添加新记录?,c#,entity-framework,oop,accounting,C#,Entity Framework,Oop,Accounting,我想在c#6和entity framework 6中创建一个简单的总账应用程序,但我想我正在努力进行正确的设计 从我的(简化的)观点来看,总账是一个交易的集合,以后可以用它来编译相关的数据/报告 我对交易的要求如下: 将交易添加/过账到总账后,为了确保完整的审计跟踪,交易不能更改或删除 仅当相应日期在某个(会计)期间时,才能添加新交易记录。还将应用其他检查(例如,借贷金额是否匹配)。 现有交易(即先前过账的交易)也应可用,但在加载时,它们不得再通过上述检查(例如,它们可能来自较早的会计期间)
- 将交易添加/过账到总账后,为了确保完整的审计跟踪,交易不能更改或删除
- 仅当相应日期在某个(会计)期间时,才能添加新交易记录。还将应用其他检查(例如,借贷金额是否匹配)。
- 现有交易(即先前过账的交易)也应可用,但在加载时,它们不得再通过上述检查(例如,它们可能来自较早的会计期间)
public class GeneralLedger
{
public int GeneralLedgerId { get; set; }
public DateTime FiscalPeriodStartDate { get; set; }
public DateTime FiscalPeriodEndDate { get; set; }
private ICollection<GeneralLedgerTransaction> _transactions;
public ICollection<GeneralLedgerTransaction> Transactions {
get
{
if (this._transactions != null) {
return this._transactions.ToList().AsReadOnly();
}
else
{
return null;
}
}
}
public void AddTransaction(GeneralLedgerTransaction trx)
{
if (trx.PostingDate < this.FiscalPeriodStartDate || trx.PostingDate > this.FiscalPeriodEndDate)
{
throw new ArgumentOutOfRangeException("invalid booking date");
}
else
{
this._transactions.Add(trx);
}
}
}
public class GeneralLedgerTransaction
{
public int GeneralLedgerTransactionId { get; set; }
public DateTime PostingDate { get; set; }
public virtual ICollection<GeneralLedgerTransactionLine> GeneralLedgerTransactionLines { get; set; }
}
public class GeneralLedgerTransactionLine
{
public int GeneralLedgerTransactionLineId { get; set; }
public int FinancialAccountId { get; set; }
public virtual FinancialAccount FinancialAccount { get; set; }
public int GeneralLedgerTransactionTypeId { get; set; }
public virtual GeneralLedgerTransactionType GeneralLedgerTransactionType { get; set; }
public decimal Amount { get; set; }
}
public class FinancialAccount
{
public int FinancialAccountId { get; set; }
public string Name { get; set; }
}
public class GeneralLedgerTransactionType
{
public int GeneralLedgerTransactionTypeId { get; set; }
public string Name { get; set; }
}
public class MyDbContext : DbContext
{
public DbSet<GeneralLedger> GeneralLedgers { get; set; }
public DbSet<GeneralLedgerTransaction> GeneralLedgerTransactions { get; set; }
public DbSet<GeneralLedgerTransactionLine> GeneralLedgerTransactionLines { get; set; }
public DbSet<FinancialAccount> FinancialAccounts { get; set; }
public DbSet<GeneralLedgerTransactionType> GeneralLedgerTransactionTypes { get; set; }
}
公共类GeneralEdge
{
public int GeneralLedgerId{get;set;}
公共日期时间FiscalPeriodStartDate{get;set;}
公共日期时间FiscalPeriodEndDate{get;set;}
私人ICollection交易;
公共ICollection事务{
得到
{
如果(this.\u transactions!=null){
返回此项。_transactions.ToList().AsReadOnly();
}
其他的
{
返回null;
}
}
}
public void AddTransaction(GeneralLedgerTransaction trx)
{
如果(trx.PostingDatethis.fiscalpiordenddate)
{
抛出新ArgumentOutOfRangeException(“无效预订日期”);
}
其他的
{
此._事务.添加(trx);
}
}
}
公共类GeneralLedgerTransaction
{
public int GeneralLedgerTransactionId{get;set;}
公共日期时间发布日期{get;set;}
公共虚拟ICollection GeneralLedgerTransactionLines{get;set;}
}
公共类GeneralLedgerTransactionLine
{
public int GeneralLedgerTransactionLineId{get;set;}
公共int FinancialAccountId{get;set;}
公共虚拟财务帐户财务帐户{get;set;}
public int GeneralLedgerTransactionTypeId{get;set;}
公共虚拟GeneralLedgerTransactionType GeneralLedgerTransactionType{get;set;}
公共十进制数{get;set;}
}
公共类财务账户
{
公共int FinancialAccountId{get;set;}
公共字符串名称{get;set;}
}
公共类GeneralLedgerTransactionType
{
public int GeneralLedgerTransactionTypeId{get;set;}
公共字符串名称{get;set;}
}
公共类MyDbContext:DbContext
{
公共DbSet GeneralLedgers{get;set;}
公共DbSet GeneralLedgerTransactions{get;set;}
public DbSet GeneralLedgerTransactionLines{get;set;}
公共数据库集财务帐户{get;set;}
public DbSet GeneralLedgerTransactionTypes{get;set;}
}
现在,由于事务
属性返回一个只读
-列表,并且每个新添加的事务都会根据日期周期规则进行检查,因此无法删除或更改任何现有事务,这一点非常有效。我还可以看到记录被保存到数据库中。到目前为止,我唯一缺少的是现有记录没有加载到\u事务中
如何使现有事务在\u transactions
中可用,而无需从域类中调用实体框架?或者有没有更好的方法(即更好的设计)来解决这个问题?经过进一步调查,我了解到我可以实现我所追求的目标,如下所示:
- 将
\u事务
从私有
更改为受保护的虚拟
- 添加
modelBuilder.Configurations.Add(新的GeneralLedger.generalledgertTransactionsMapper())代码>到建模时的
- 正在创建以下配置类:
public类GeneralLedgerTransactionsMapper:EntityTypeConfiguration
{
公共GeneralLedgertTransactionsMapper()
{
有许多(s=>s.\U交易);
}
}
简单地说,您希望允许添加新记录(如果它们满足某些条件),但现有记录不应被修改,对吗?好的,既不修改也不删除,但除此之外,我认为基本上就是这样。您如何绑定数据?是否将其放入数据网格中?它在网页上吗?Windows应用程序?对不起,忘了提了。这将是一个ASP.NET MVC应用程序。如果这是一个MVC应用程序,您不能简单地删除编辑和删除控制器吗?相反,只需保留详细信息、索引和创建控制器即可。这将允许用户将项目输入分类账,但不允许修改和/或删除它们。
public class GeneralLedgerTransactionsMapper : EntityTypeConfiguration<GeneralLedger>
{
public GeneralLedgerTransactionsMapper()
{
HasMany(s => s._transactions);
}
}