|
| 1 | +// Copyright (C) 2020 Xtensive LLC. |
| 2 | +// All rights reserved. |
| 3 | +// For conditions of distribution and use, see license. |
| 4 | +// Created by: Alexey Kulakov |
| 5 | +// Created: 2020.02.14 |
| 6 | + |
| 7 | +using System; |
| 8 | +using System.Collections.Generic; |
| 9 | +using System.Text; |
| 10 | +using NUnit.Framework; |
| 11 | +using Xtensive.Orm.Configuration; |
| 12 | +using Xtensive.Orm.Validation; |
| 13 | +using Xtensive.Orm.Tests.Issues.IssueJira0792_UnableToRemoveAssignedEntityWithNonNullableAssociationFieldModel; |
| 14 | + |
| 15 | +namespace Xtensive.Orm.Tests.Issues.IssueJira0792_UnableToRemoveAssignedEntityWithNonNullableAssociationFieldModel |
| 16 | +{ |
| 17 | + [HierarchyRoot] |
| 18 | + public class Job : Entity |
| 19 | + { |
| 20 | + [Field, Key] |
| 21 | + public long Id { get; private set; } |
| 22 | + |
| 23 | + [Field] |
| 24 | + [Association(PairTo = nameof (JobTechnology.ReqiredJob), OnTargetRemove = OnRemoveAction.Clear, OnOwnerRemove = OnRemoveAction.Cascade)] |
| 25 | + public JobTechnology Technology { get; set; } |
| 26 | + |
| 27 | + public Job(Session session) |
| 28 | + : base(session) |
| 29 | + { |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + [HierarchyRoot] |
| 34 | + public class JobTechnology : Entity |
| 35 | + { |
| 36 | + [Field, Key] |
| 37 | + public long Id { get; set; } |
| 38 | + |
| 39 | + [Field(Nullable = false), NotNullConstraint] |
| 40 | + public Job ReqiredJob { get; set; } |
| 41 | + |
| 42 | + public JobTechnology(Session session, Job job) |
| 43 | + : base(session) |
| 44 | + { |
| 45 | + ReqiredJob = job; |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +namespace Xtensive.Orm.Tests.Issues |
| 51 | +{ |
| 52 | + public class IssueJira0792_UnableToRemoveAssignedEntityWithNonNullableAssociationField : AutoBuildTest |
| 53 | + { |
| 54 | + protected override DomainConfiguration BuildConfiguration() |
| 55 | + { |
| 56 | + var configuration = base.BuildConfiguration(); |
| 57 | + configuration.Types.Register(typeof (Job).Assembly, typeof (Job).Namespace); |
| 58 | + configuration.UpgradeMode = DomainUpgradeMode.Recreate; |
| 59 | + return configuration; |
| 60 | + } |
| 61 | + |
| 62 | + protected override void PopulateData() |
| 63 | + { |
| 64 | + using (var session = Domain.OpenSession()) |
| 65 | + using (var transaction = session.OpenTransaction()) { |
| 66 | + new JobTechnology(session, new Job(session)); |
| 67 | + new JobTechnology(session, new Job(session)); |
| 68 | + new JobTechnology(session, new Job(session)); |
| 69 | + new JobTechnology(session, new Job(session)); |
| 70 | + |
| 71 | + transaction.Complete(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + [Test] |
| 76 | + public void MainTest() |
| 77 | + { |
| 78 | + using (var session = Domain.OpenSession()) |
| 79 | + using (var transaction = session.OpenTransaction()) { |
| 80 | + foreach (var job in session.Query.All<Job>()) { |
| 81 | + job.Technology.Remove(); |
| 82 | + session.SaveChanges(); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments