尝试将实体的状态更改为“已修改”时,实体类型“”的属性“”具有临时值

时间:2020-08-11 05:17:36

标签: asp.net-core-3.1 ef-core-3.1

我有一对多的关系。我相信我的设置正确,请参见下面的代码。一批将包含一堆帐户。当所有对象都是新对象并立即插入数据库时​​,一切工作正常。但是,在某些情况下,在与批处理关联之前先保存帐户。然后,稍后创建一个批次,将现有的无批次帐户添加到该批次中。尝试保存时,出现以下错误:尝试将实体的状态更改为“已修改”时,实体类型“帐户”的属性“ BatchId”具有临时值。要么显式设置一个永久值,要么确保将数据库配置为为此属性生成值。

我正在将ASP.Net Core 3.1和EF Core 3.1.6用于SQLServer。

public class Batch
{
    private List<Account> _accounts;
    
    public int BatchId { get; set; }
    public int AccountCount { get; internal set; }
    public decimal AccountTotal { get; internal set; }
    public virtual IReadOnlyCollection<Account> Accounts => _accounts?.AsReadOnly( );
    
    public void AddAccount(Account account)
    {
        if( account == null )
            return;

        if( _accounts == null )
            _accounts = new List<Account>( );

        _accounts.Add( account );
        AccountCount++;
        AccountTotal += account.OriginalBalance;
    }
}

public class Account
{
    public int AccountId { get; set; }
    public decimal OriginalBalance { get; set; }
}

public class BatchConfig : IEntityTypeConfiguration<Batch>
{
    public void Configure( EntityTypeBuilder<Batch> builder )
    {
        if( builder == null )
            throw new ArgumentNullException( nameof( builder ) );

        builder.ToTable( "XXXXX" ).HasKey( e => e.BatchId );

        builder.Property( e => e.BatchId ).UseIdentityColumn( );

        builder
            .HasMany( e => e.Accounts )
            .WithOne( )
            .HasForeignKey( e => e.BatchId )
            .HasPrincipalKey( e => e.BatchId )
            ;

    }
}

public class AccountConfig : IEntityTypeConfiguration<Account>
{
    public void Configure( EntityTypeBuilder<Account> builder )
    {
        if( builder == null )
            throw new ArgumentNullException( nameof( builder ) );

        builder.ToTable( "XXXXX" ).HasKey( e => e.AccountId );
        builder.Property( e => e.AccountId ).UseIdentityColumn( );

        builder.Property( e => e.TotalAdjustments ).HasComputedColumnSql( "TotalAdjustments" );
        builder.Property( e => e.TotalDisbursements ).HasComputedColumnSql( "TotalDisbursements" );

        builder.Property( e => e.BatchId ).HasColumnName( "DepositBatchId" );

        builder
            .HasOne( e => e.CheckPayerCompany )
            .WithOne( )
            .HasForeignKey<Account>( e => e.CheckCompanyId );       

        builder
            .HasOne( e => e.AccountType )
            .WithOne( )
            .HasForeignKey<Account>( e => e.AccountTypeId );

        builder
            .HasOne( e => e.DepositTypeLookup )
            .WithOne( )
            .HasForeignKey<ClipAccount>( e => e.DepositType );

        builder
            .HasOne( e => e.EmployeeContribPercentage )
            .WithOne( )
            .HasForeignKey<Account>( e => e.EECPercentId );

        builder
            .HasOne( e => e.EmployerPensionContribPercentage )
            .WithOne( )
            .HasForeignKey<Account>( e => e.ERPPercentId );

        builder
            .HasOne( e => e.EmployerHealthContribPercentage )
            .WithOne( )
            .HasForeignKey<Account>( e => e.ERHPercentId );
    }
}

0 个答案:

没有答案
相关问题