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

CreateUpdateDDLScript not generate Constraints And Foregein Keys

3 Answers 107 Views
Data Access Free Edition
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Ronaldo
Top achievements
Rank 2
Ronaldo asked on 14 Oct 2012, 07:14 AM
Hello community,


I'm using in my project a model Fluent generated by the wizard, using the migration process, my script is generated normally but my foreign keys are not generated. I check all propertys in model.

The following code:
public string AdaptarBancoDados()
        {
            string script = null;
            using (EntitiesModel dbContext = new EntitiesModel())
            {
                Telerik.OpenAccess.ISchemaHandler schemaHandler = dbContext.GetSchemaHandler();
                 
                if (schemaHandler.DatabaseExists())
                {
                    script = schemaHandler.CreateUpdateDDLScript(null);
                }
                else
                {
                    schemaHandler.CreateDatabase();
                    script = schemaHandler.CreateDDLScript();
                }
            }
            return script;
        }

3 Answers, 1 is accepted

Sort by
0
Doroteya
Telerik team
answered on 16 Oct 2012, 12:14 PM
Hi Ronaldo,

Generally, in Telerik OpenAccess ORM, when you map associations and update your database the way you do, the foreign key constraints will not be created. In order to generate the script for the constraints, you should override the CreateModel() method in your FluentMetadataSource class.

The best practice in this case is:
    - Add a new class to your project
    - Name it EntitesModelFluentMetadataSource.partial.cs
    - Open it and make sure you have "public partial class" before the name of the class, it will be a partial class for your FluentMetadataSource
    - Inside the class, you have to override the default value we are setting to the NullForeignKey property, which is preventing the creation of constraints. In order to do that, paste the following
protected override Telerik.OpenAccess.Metadata.MetadataContainer CreateModel()
        {
            MetadataContainer container = base.CreateModel();
            container.DefaultMapping.NullForeignKey = true;
            return container;
        }
    - Rebuild the assembly

Note that if you have built your model using Model First scenario you will have to do additional configuration of the association in the generated code (check here).

I also suggest you to check the best practice for using fluent code generation described here.


Kind regards,
Doroteya
the Telerik team
Telerik OpenAccess ORM Meets ASP.NET Web API. Read more.
0
Elisabeth Ohmstead
Top achievements
Rank 1
answered on 17 Apr 2013, 04:43 PM
Hello,

Im using similar code to update my database schema but the script returned at line "script = schemaHandler.CreateUpdateDDLScript(null);" always returns null reference and "script = schemaHandler.CreateDDLScript();" always return an empty string. 

Could you please let me know what to do in my case? Here is my code:

private static void UpdateDatabase()
        {
            using (var context = new EntitiesModel())
            {
                var schemaHandler = context.GetSchemaHandler();
                EnsureDB(schemaHandler);
            }
        }
 
        private static void EnsureDB(ISchemaHandler schemaHandler)
        {
            string script = null;
            if (schemaHandler.DatabaseExists())
            {
                script = schemaHandler.CreateUpdateDDLScript(null);
            }
            else
            {
                schemaHandler.CreateDatabase();
                script = schemaHandler.CreateDDLScript();
            }
            if (!string.IsNullOrEmpty(script))
            {
                schemaHandler.ExecuteDDLScript(script);
            }
        }
0
Yordan
Telerik team
answered on 22 Apr 2013, 02:17 PM
Hello Elisabeth,

Please find the answer to the same question in this forum thread

Should you face any further difficulties regarding that problem I suggest we continue the discussion there.

Regards,
Yordan
the Telerik team
Using Encrypted Connection Strings with Telerik OpenAccess ORM. Read our latest blog article >>
Tags
Data Access Free Edition
Asked by
Ronaldo
Top achievements
Rank 2
Answers by
Doroteya
Telerik team
Elisabeth Ohmstead
Top achievements
Rank 1
Yordan
Telerik team
Share this question
or