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

问题描述

我有一对多的关系。我相信我的设置正确,请参见下面的代码。一批将包含一堆帐户。当所有对象都是新对象并立即插入数据库时​​,一切工作正常。但是,在某些情况下,在与批处理关联之前先保存帐户。然后,稍后创建一个批次,将现有的无批次帐户添加到该批次中。尝试保存时,出现以下错误尝试将实体的状态更改为“已修改”时,实体类型“帐户”的属性“ 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 );
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)