Why are tables created that are not in the model?

4 posts, 0 answers
  1. John
    John avatar
    2 posts
    Member since:
    Sep 2013

    Posted 29 Dec 2013 Link to this post

    I'm trying to use some inheritance, but then I get some additional tables.

    So for example:
    The model three entities:
    1. A: Contains only Id
    2. B: Contains Id and AId, associated to A
    3. A1: Contains naam and inherits from A

    The context when using designer will have collection properties:
    1. As
    2. Bs
    3. A1s

    If I then run it and add data the tables will be A, B and A1 that's ok.
    If I then remove the As and run and add data I will have table A, B, A1 and A_B.
    A_B will contain Id, seq and Id2.

    So why is A_B created?
    What I'm trying to accomplish using A generic type that is passed to the constructor of the context and used for the constructor.
    But that will create the additional table, I know I could add another property with the collection of the base type.

  2. Boris Georgiev
    Admin
    Boris Georgiev avatar
    190 posts

    Posted 30 Dec 2013 Link to this post

    Hi John,

    Unfortunately we were unable to reproduce the wrong behavior on our side. Could you please provide us more information about your scenario:
    1) How did you delete the "As" collection?
    2) Did you change the association from A-B to A1-B?
    3) How do you want to pass the generic type of A to the context constructor? 
    4) Where you want to add "another property with the collection of the base type"? 

    The table created table A_B is a join table. But to generate such a table, the association should be changed from one-to-many to many-to-many. Did you make any changes in the association?

    Answering these questions or if you can provide a small sample demonstrating the problem will help us greatly to understand the issue and help you in the best possible way. 

    We are looking forward to your feedback.

    Regards,
    Boris Georgiev
    Telerik
    OpenAccess ORM Q3 2013 simplifies your model operations even further providing you with greater flexibility. Check out the list of new features shipped with our latest release!
  3. DevCraft banner
  4. John
    John avatar
    2 posts
    Member since:
    Sep 2013

    Posted 01 Jan 2014 Link to this post

    This example code below will create that table.
    If the As is enable the a_b table won't be created.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Telerik.OpenAccess;
    using Telerik.OpenAccess.Metadata;
     
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                TestModel tm = new TestModel();
                var sh = tm.GetSchemaHandler();
                string script = null;
                if (sh.DatabaseExists())
                {
                    script = sh.CreateUpdateDDLScript(null);
                }
                else
                {
                    sh.CreateDatabase();
                    script = sh.CreateDDLScript();
                }
                if (!string.IsNullOrEmpty(script))
                {
                    sh.ExecuteDDLScript(script);
                
     
                A1 a = new A1(){Id="A1"};
                B b = new B(){Id="B", AId="A1"};
                tm.Add(a);
                tm.Add(b);
                tm.SaveChanges();
            }
        }
     
        public partial class TestModel : OpenAccessContext
        {
            private static string connectionStringName = @"DefaultConnection";
     
            private static BackendConfiguration backend = new BackendConfiguration()
            {
                Backend = "MsSql",
                ProviderName = "System.Data.SqlClient"
            };
                     
            private static MetadataSource metadataSource = AttributesMetadataSource.FromContext(typeof(TestModel));
             
            public TestModel()
                :base(connectionStringName, backend, metadataSource)
            { }
                 
            /*
            public IQueryable<A> As
            {
                get
                {
                    return this.GetAll<A>();
                }
            }*/
             
            public IQueryable<A1> A1
            {
                get
                {
                    return this.GetAll<A1>();
                }
            }
             
            public IQueryable<B> Bs
            {
                get
                {
                    return this.GetAll<B>();
                }
            }
             
        }
     
        [Table(UpdateSchema = true)]
        public partial class A
        {
            private string _id;
            [Column(IsPrimaryKey = true)]
            [Storage("_id")]
            public virtual string Id
            {
                get
                {
                    return this._id;
                }
                set
                {
                    this._id = value;
                }
            }
     
            private IList<B> _bs = new List<B>();
            [Collection(InverseProperty = "A")]
            [Storage("_bs")]
            public virtual IList<B> Bs
            {
                get
                {
                    return this._bs;
                }
            }
     
        }
     
        [Table(UpdateSchema = true)]
        [InheritanceMapping(InheritanceStrategy = InheritanceStrategy.Vertical)]
        public partial class A1 : A
        {
            private string _name;
            [Storage("_name")]
            public virtual string Name
            {
                get
                {
                    return this._name;
                }
                set
                {
                    this._name = value;
                }
            }
     
        }
     
        [Table(UpdateSchema = true)]
        public partial class B
        {
            private string _aId;
            [Storage("_aId")]
            public virtual string AId
            {
                get
                {
                    return this._aId;
                }
                set
                {
                    this._aId = value;
                }
            }
     
            private string _property1;
            [Storage("_property1")]
            public virtual string Property1
            {
                get
                {
                    return this._property1;
                }
                set
                {
                    this._property1 = value;
                }
            }
     
            private string _id;
            [Column(IsPrimaryKey = true)]
            [Storage("_id")]
            public virtual string Id
            {
                get
                {
                    return this._id;
                }
                set
                {
                    this._id = value;
                }
            }
     
            private A _a;
            [ForeignKeyAssociation(SharedFields = "AId", TargetFields = "Id")]
            [Storage("_a")]
            public virtual A A
            {
                get
                {
                    return this._a;
                }
                set
                {
                    this._a = value;
                }
            }
     
        }
     
    }
  5. Kristian Nikolov
    Admin
    Kristian Nikolov avatar
    206 posts

    Posted 02 Jan 2014 Link to this post

    Hello John,

    Thank you for the provided code. Based on it we were able to reproduce the problem you are experiencing. This is not the expected behavior of OpenAccess ORM and unfortunately it appears that it is a bug in our Attributes Mapping. We have logged it in our backlog.

    We can suggest you three approaches which would allow you to circumvent the issue:
    • Change the mapping of your Domain Model to XML Mapping.
    • If you prefer your mapping to be defined with code, you can use a Fluent Model without an .rlinq file.
    • Should you require your model to have an .rlinq file and use code for the mapping, you can change the mapping of your Domain Model to Fluent. Be aware that this approach is not recommended as it removes one of the main advantages of the Fluent Mapping - one time code generation which allows for higher customization. For more information about changing the mapping of your Domain Model to Fluent, you can refer to this article.

    Also note that the issue does not seem to appear when you create the database using our Update Database from Model wizard. This may be a viable solution as well if it is applicable in your scenario.

    Please find your Telerik Points updated for finding this bug and accept our apologies for the inconvenience caused.

    If you have additional questions, feel free to post in our forums again.

    Regards,
    Kristian Nikolov
    Telerik
    OpenAccess ORM Q3 2013 simplifies your model operations even further providing you with greater flexibility. Check out the list of new features shipped with our latest release!
Back to Top