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

fluent many-to-many problem

3 Answers 71 Views
Getting Started
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
MATEUSZ
Top achievements
Rank 1
MATEUSZ asked on 21 Dec 2011, 01:57 PM
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;
    }
}

3 Answers, 1 is accepted

Sort by
0
PetarP
Telerik team
answered on 23 Dec 2011, 03:24 PM
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!

0
MATEUSZ
Top achievements
Rank 1
answered on 25 Dec 2011, 09:04 PM
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

0
Accepted
Serge
Telerik team
answered on 28 Dec 2011, 05:20 PM
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!

Tags
Getting Started
Asked by
MATEUSZ
Top achievements
Rank 1
Answers by
PetarP
Telerik team
MATEUSZ
Top achievements
Rank 1
Serge
Telerik team
Share this question
or