This is a migrated thread and some comments may be shown as answers.

Droppping self foreign key.

1 Answer 42 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Dmitry
Top achievements
Rank 1
Dmitry asked on 19 Jun 2013, 06:54 AM
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.

1 Answer, 1 is accepted

Sort by
0
Damyan Bogoev
Telerik team
answered on 24 Jun 2013, 06:31 AM
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.
Tags
Development (API, general questions)
Asked by
Dmitry
Top achievements
Rank 1
Answers by
Damyan Bogoev
Telerik team
Share this question
or