Droppping self foreign key.

2 posts, 0 answers
  1. Dmitry
    Dmitry avatar
    8 posts
    Member since:
    Oct 2012

    Posted 19 Jun 2013 Link to this post

    I have a table with self foreign key.
    CREATE TABLE [dbo].[Test](
        [Id] [int] IDENTITY(1,1) NOT NULL,
        [ID_Parent] [int] NULL,
        [Code] [varchar](100) NULL,
     CONSTRAINT [PK_Test] PRIMARY KEY CLUSTERED
    (
        [Id] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF,
           ALLOW_ROW_LOCKS  =
    ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]) ON [PRIMARY]
     
    ALTER TABLE [dbo].[Test]  WITH CHECK ADD  CONSTRAINT [FK_Test_Test] FOREIGN KEY([ID_Parent])
    REFERENCES [dbo].[Test] ([Id])
    ALTER TABLE [dbo].[Test] CHECK CONSTRAINT [FK_Test_Test]
    For mapping I'm using Artificial:
    MappingConfiguration configuration = new MappingConfiguration("Test", "ConsoleApp");
    configuration.MapType().WithConcurencyControl(OptimisticConcurrencyControlStrategy.Changed).ToTable("Test");
    var prop = configuration.HasArtificialAssociation(
    "ID_Parent_fk", configuration.ConfiguredType);
    prop.WithOppositeCollection(
    "Tests");
    prop.ToColumn("ID_Parent");
    prop.HasConstraint();
    But handler.CreateUpdateDDLScript generate script for deleting constraint.
    var handler = GetSchemaHandler();
    //script = 'ALTER TABLE [Test] DROP CONSTRAINT [FK_Test_Test]'
    var script = handler.CreateUpdateDDLScript(
    new SchemaUpdateProperties());
    if (!String.IsNullOrEmpty(script))
        handler.ForceExecuteDDLScript(script);

    How map self foreign key right using artificial mapping?
    Thanks.

  2. Damyan Bogoev
    Admin
    Damyan Bogoev avatar
    581 posts

    Posted 24 Jun 2013 Link to this post

    Hi Dmitry,

     
    I am afraid this is an expected behaviour - dropping existing self-reference constraints.

    Actually you could disable generating any migration script related to constraints by setting the SchemaUpdateProperties CheckConstraint setting to false.

    Hope that helps.

    Regards,
    Damyan Bogoev
    Telerik
    OpenAccess ORM Q2 2013 brings you a more powerful code generation and a unique Bulk Operations support with LINQ syntax. Check out the list of new functionality and improvements shipped with this release.
  3. DevCraft banner
Back to Top