code only mapping to make table name a variable

4 posts, 0 answers
  1. Chom
    Chom avatar
    56 posts
    Member since:
    Dec 2009

    Posted 11 Mar 2014 Link to this post

    Hello,

    I am creating my schema using code only and I need to create several tables using the same table schema only with a different name. I need to add a variable string for MappingConfiguration ToTable("mystring") method. In other words I need to have ToTable(MyVariable). I am not sure how to
    achieve this. The problem is when I call UpdateSchema(), the variable logic is somehow lost when creating the table mapping. Below is an idea of what I am doing.

      protected override Telerik.OpenAccess.Metadata.MetadataContainer CreateModel()
    {
    Telerik.OpenAccess.Metadata.MetadataContainer model = base.CreateModel();
    model.DefaultMapping.NullForeignKey = true;
    return model;
    }

    protected override IList<MappingConfiguration> PrepareMapping()
    {
    List<MappingConfiguration> config = new List<MappingConfiguration>();

    MappingConfiguration<Template> template = new MappingConfiguration<Template>();
    template.MapType(x => new
    {
    Id = x.Id,
    Name = x.Name,
    Value = x.Value
    }).ToTable(MyVariable);
    template.HasProperty(p => p.Id).IsIdentity();
    template.HasProperty(p => p.Name).IsNotNullable().IsUnicode().HasLength(100);
    template.HasProperty(p => p.Value).IsNullable().IsUnicode().HasLength(100);

    config.Add(template);

    return config;
    }
    }

  2. Boris Georgiev
    Admin
    Boris Georgiev avatar
    190 posts

    Posted 13 Mar 2014 Link to this post

    Hello Gavin,

    In order for us to understand your scenario and assist you in the best possible way, could you please provide us some more information:
    1) How do you want to pass the variable to the PrepareMapping() method?
    2) Do you want to use the variable only once to update the database schema?
    3) Do you want to change the variable and the mapping at runtime?

    I also suggest you to take a look at the Artificial Types API provided by Telerik Data Access which gives you the opportunity to your persistent model during the runtime.

    I am looking forward to hearing from you.

    Regards,
    Boris Georgiev
    Telerik
     
    OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
     
  3. DevCraft banner
  4. Chom
    Chom avatar
    56 posts
    Member since:
    Dec 2009

    Posted 19 Mar 2014 in reply to Boris Georgiev Link to this post

    Hi Boris,
    I was able to create the tables using script and executenonquery

    ​ using (DataLogFluentContext db = new DataLogFluentContext(ConnectionType))
    {
    db.ExecuteNonQuery("CREATE TABLE " + tables etc);
                    db.SaveChanges();
    }

    but I would like to have the option to select between different databases to create tables. What is the best way to send a small demo app?

    Thanks

    Gavin
  5. Boris Georgiev
    Admin
    Boris Georgiev avatar
    190 posts

    Posted 19 Mar 2014 Link to this post

    Hello Gavin,

    I am glad to see that you have found a solution. 

    The forum thread is changed into a Product Feedback ticket, so you are now allowed to add attachments.

    I am looking forward to hearing from you.

    Regards,
    Boris Georgiev
    Telerik
     
    OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
     
Back to Top