question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Repository.InsertOrUpdate() insert duplicate principal record in AbpTestBase of one to zero relationships

See original GitHub issue

AbpTestBase 3.5 version MVC 5

Description:

I recreate a new UT (AbpTest Base 3.5 version), A:B (1:0-1)


    [Table("A")]
    public class A : FullAuditedAggregateRoot<long>
    {
        public virtual B B { get; set; }
    }


    [Table("B")]
    public class B : FullAuditedAggregateRoot<long>
    {
        [ForeignKey("Id")]
        [Required]
        public virtual A A { get; set; }

        public B()
        {
        }
    }

private readonly IRepository<A, long> _aRepository;

        private readonly IRepository<B, long> _bRepository;

        public TestClassTests()
        {
            _aRepository = LocalIocManager.Resolve<IRepository<A, long>>();
            _bRepository = LocalIocManager.Resolve<IRepository<B, long>>();
        }


 [TestMethod()]
        [UnitOfWork]
        public void BTest()
        {
            var a = new A();
            _aRepository.InsertOrUpdate(a); //will insert a A record to A-tables

            var b = new B();
            b.A = a;
            var tempB = _bRepository.InsertOrUpdate(b); //will insert A to A-tables  and B to B-tables, there are two A records in A-tables.
        }

Question: _bRepository.InsertOrUpdate(b); //will insert A to A-tables and B to B-tables, there are two A records in A-tables.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
ismcagdascommented, Mar 21, 2018

@muzizongheng thanks for sharing the project. Unit test methods are not unit of work. So, change your test class like below;

public class TestDemo_Tests : AbpTestDemoTestBase
{
	private IUnitOfWorkManager _unitOfWorkManager;

	private readonly IRepository<A, long> _aRepository;
	private readonly IRepository<B, long> _bRepository;

	public TestDemo_Tests()
	{
		_unitOfWorkManager = Resolve<IUnitOfWorkManager>();
		_aRepository = LocalIocManager.Resolve<IRepository<A, long>>();
		_bRepository = LocalIocManager.Resolve<IRepository<B, long>>();
	}

	[Fact]
	public void BTest()
	{
		using (var uow = _unitOfWorkManager.Begin())
		{
			var a = new A();
			_aRepository.InsertOrUpdateAndGetId(a);

			var b = new B();
			b.A = a;
			_bRepository.InsertOrUpdateAndGetId(b);

			uow.Complete();

			a = _aRepository.Get(a.Id);
			b = _bRepository.Get(b.Id);
		}
	}
}

If you place this code in an application service, it will be unit of work by default and you will not have such a problem.

5reactions
acjhcommented, Mar 21, 2018

I could not reproduce this.

But if you want a method to be grouped in one UnitOfWork, mark it as virtual:

[UnitOfWork]
public virtual void BTest()
Read more comments on GitHub >

github_iconTop Results From Across the Web

Duplicate Record in one to one relationship when ...
i have a one to one relationship between two entity and whn i want set navigation property add duplication record tu my table....
Read more >
MySQL insert on duplicate update for non-PRIMARY key
This is the query: UPDATE my_table SET unique_name = 'one', update_datetime = NOW() WHERE id = 1; INSERT INTO my_table SELECT 1, 'one',...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found