CreateUpdateDDLScript not generate Constraints And Foregein Keys

4 posts, 0 answers
  1. Ronaldo
    Ronaldo avatar
    2 posts
    Member since:
    Oct 2012

    Posted 14 Oct 2012 Link to this post

    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;
            }

  2. Doroteya
    Admin
    Doroteya avatar
    502 posts

    Posted 16 Oct 2012 Link to this post

    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.
  3. DevCraft banner
  4. Elisabeth Ohmstead
    Elisabeth Ohmstead avatar
    4 posts
    Member since:
    Apr 2007

    Posted 17 Apr 2013 Link to this post

    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);
                }
            }
  5. Yordan
    Admin
    Yordan avatar
    39 posts

    Posted 22 Apr 2013 Link to this post

    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 >>
Back to Top