fluent many-to-many problem

4 posts, 1 answers
  1. MATEUSZ
    MATEUSZ avatar
    18 posts
    Member since:
    Jul 2011

    Posted 21 Dec 2011 Link to this post

    Hi.
    I have two classes: employee and order.
    I need many-to-many relationship between them: Employee has PerformedExportations property and Order has ExportationPerformers.
    It turned out that such a configuration doesn't work. It's probably caused by the fact, that relationship definition:
    mapping.HasAssociation(o => o.ExportationPerformers)
        .WithOpposite(e => e.PerformedExportations)
        .MapJoinTable("ExportationsPerformers", (o, e) => new
        {
            OrderId = o.Id,
            EmployeeId = e.Id
        }
    );
    didn't result in creating ExportationPerformers table in a database. I had to create support-class ExportationPerformer containing EmployeeId and OrderId, but it's just a workaround and I'd like to use build-in many-to-many relationship mechanism.

    So, what am I doing wrong and how should this piece of code be written to work properly?


    public class Employee
    {
        public int Id { get; set; }
     
        private TrackedBindingList<Order> performedExportations = new TrackedBindingList<Order>();
        public IList<Order> PerformedExportations
        {
            get { return performedExportations; }
        }
     
        public static MappingConfiguration GetMapping()
        {
            var mapping = new MappingConfiguration<Employee>();
            mapping.MapType(u => new
            {
                Id = u.Id,
                ...other fields
            }).ToTable("Employees");
     
            mapping.HasProperty(u => u.Id).IsIdentity(KeyGenerator.Autoinc);
            return mapping;
        }
    }
     
    public class Order
    {
        public int Id { get; set; }
     
        private TrackedBindingList<Employee> exportationPerformers = new TrackedBindingList<Employee>();
        public IList<Employee> ExportationPerformers
        {
            get { return exportationPerformers; }
        }
     
        public static MappingConfiguration GetMapping()
        {
            var mapping = new MappingConfiguration<Order>();
            mapping.MapType(o => new
            {
                Id = o.Id,
                ...other fields
            }).ToTable("Orders");
     
            mapping.HasProperty(o => o.Id).IsIdentity(KeyGenerator.Autoinc);
     
            mapping.HasAssociation(o => o.ExportationPerformers)
                .WithOpposite(e => e.PerformedExportations)
                .MapJoinTable("ExportationsPerformers", (o, e) => new
                {
                    OrderId = o.Id,
                    EmployeeId = e.Id
                }
            );
     
            return mapping;
        }
    }
  2. PetarP
    Admin
    PetarP avatar
    754 posts

    Posted 23 Dec 2011 Link to this post

    Hi,

     Basically what you have written should work with no problem. Can you please share with me the version you are using, because it is possible that the specific version of yours could contain a bug.
    I am attaching a working example that demonstrates the same scenario where the join table is successfully created.

    All the best,
    Petar
    the Telerik team

    Q3’11 of Telerik OpenAccess ORM is available for download. Register for the What's New in Data Tools webinar to see what's new and get a chance to WIN A FREE LICENSE!

  3. DevCraft banner
  4. MATEUSZ
    MATEUSZ avatar
    18 posts
    Member since:
    Jul 2011

    Posted 25 Dec 2011 Link to this post

    have you really checked code you attached?

    at first I thought it could be a problem with sqlite and propagated model to sqlite database. it created only 2 tables: products&order. then i fired your code with the same result.

    i'm using newest openaccess q3, .net 4, visual 2k10, windows7, sqlite/mssqlserver2008r2/mssqlexpress.

    simply, even though mapjointable code is fire, it doesnt create "the joining table", which can be seen in create script:

    -- FluentModel.Order
    CREATE TABLE [Orders] (
        [order_id] int IDENTITY NOT NULL,       -- orderId
        CONSTRAINT [pk_Orders] PRIMARY KEY ([order_id])
    )
    go


    -- FluentModel.Product
    CREATE TABLE [Products] (
        [ID] int IDENTITY NOT NULL,             -- <ID>k__BackingField
        [Price] NUMERIC(20,10) NOT NULL,        -- <Price>k__BackingField
        [ProductName] varchar(255) NULL,        -- <ProductName>k__BackingField
        CONSTRAINT [pk_Products] PRIMARY KEY ([ID])
    )
    go

  5. Answer
    Serge
    Admin
    Serge avatar
    375 posts

    Posted 28 Dec 2011 Link to this post

    Hello,

     Can you verify that you are using the latest build from Telerik OpenAccess ORM. The Q3 release contains a critical bug in the many-to-many handling of association that are mapped using the Fluent API. This is why a service pack was released in the following week. Please download the latest version from our site (or this link, though you will have to be logged in first).

    I am looking forward to resolving this issue with you. 

    Greetings,
    Serge
    the Telerik team

    Q3’11 of Telerik OpenAccess ORM is available for download. Register for the What's New in Data Tools webinar to see what's new and get a chance to WIN A FREE LICENSE!

Back to Top