How can we do that?


//Source class
[Serializable]
 [HierarchyRoot]
 public class Client : Entity
 {
  [Field, Key]
  public int Id { get; set; }

  [Field]
  public string Name { get; set; }

[...]
  [Field]
  public DateTime RegistrationDate { get; private set; }

  [Field]
  public DateTime BirthDate { get; set; }
[...]
}

//source class, that uses Client

 [HierarchyRoot()]
 [Serializable]
 public class ProductCheckAction : Entity
 {
  [Field, Key]
  public int Id { get; set; }
[...]
  [Field]
  public ProductCheckFields Fields { get; set; }
[...]
}

 public class ProductCheckFields : Structure
 {
  [Field]
  public Client Client { get; set; }
[...]
 }

===
//new Superclass
 [Serializable]
    [HierarchyRoot(InheritanceSchema = InheritanceSchema.ConcreteTable)]
    public abstract class Contractor : Entity
    {
        [Field, Key]
        public int Id { get; set; }

        [Field]
        public string Name { get; set; }
    }

//modified Client class
[Serializable]
 public class Client : Contractor
 {
     [Field]
  public DateTime RegistrationDate { get; private set; }

     [Field]
  public DateTime BirthDate { get; set; }
[...]
}

===

===
//upgdade hints
hints.Add(new MoveFieldHint(typeof (Client), "Id", typeof (Contractor)));
hints.Add(new MoveFieldHint(typeof(Client), "Name", typeof(Contractor)));
===

But DO delete all clients at first, then we get error:


Xtensive.Orm.ReferentialConstraintViolationException: SQL error occured.

Storage error details 'Entity: Client; Field: Id (FieldInfo);'

ALTER TABLE [dbo].[ProductCheckAction] ADD CONSTRAINT [FK_ProductCheckAction_Fields.Client_Client] FOREIGN KEY ([Fields.Client]) REFERENCES [dbo].[Client] ([Id]);

Original message 'The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_ProductCheckAction_Fields.Client_Client". The conflict occurred in database "shop", table "dbo.Client", column 'Id'.' ---> System.Data.SqlClient.SqlException: The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_ProductCheckAction_Fields.Client_Client". The conflict occurred in database "shop", table "dbo.Client", column 'Id'.

update:

There is similar error in some cases (for example, when simply add one reference) and doing PerformSafely:

UPDATE [dbo].[Debenture] SET [Supplier.Id] = DEFAULT WHERE EXISTS (SELECT [aProductSupplier].[Id] FROM [dbo].[ProductSupplier] [aProductSupplier] WHERE ([aProductSupplier].[Id] = [Debenture].[Supplier.Id]));

DELETE FROM [dbo].[ProductSupplier];'

Original message 'The DELETE statement conflicted with the REFERENCE constraint "FK_PrimaryIncomingWaybillAction_Fields.Supplier_ProductSupplier". The conflict occurred in database "Drezex_B_Major", table "dbo.PrimaryIncomingWaybillAction", column 'Fields.Supplier'. The statement has been terminated.'

DO deletes everything from ProductSupplier and leaves database broken.

Very strange: why PerformSafely makes queries that can DELETE all data from some table? I think, it is not safely action :)

asked Apr 20 '12 at 05:44

Ness's gravatar image

Ness
155232328

edited Apr 28 '12 at 09:16

I've updated post with some strange PeformSafely action that is similar to main error.

(Apr 28 '12 at 09:18) Ness Ness's gravatar image

One Answer:

Hello Ness,

You don't need upgrade hints at all.

When you add new type at the root of hierarchy (Client) in ConcreteTable mode, old hierarchy root (Contractor) table does not change, DO will simply add new table with required columns.

However your exception is strange, ConcreteTable hierarchy could not be target of FK and thus DO disables such FK unless hierarchy has exactly one type.

answered Apr 24 '12 at 04:38

Denis%20Krjuchkov's gravatar image

Denis Krjuchkov
179325

We have tried without upgrade hints, there is the exact same error.

When doing perform DO clears all Client table (old table), so when trying to alter ProductCheckAction we get error: there are no clients but ProductCheckActions have Client.Id field with values that are not in the Client table.

(Apr 25 '12 at 03:54) Ness Ness's gravatar image

I can't reproduce the problem with your sample. Could you please send me full upgrade log to denis at dataobjects net, so I can investigate further.

(Apr 25 '12 at 04:56) Denis Krjuchkov Denis%20Krjuchkov's gravatar image
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

powered by OSQA