1
votes

What is wrong with my class mappings below?

public class Foo
{
    public Foo()
    {
        ActualCurve = new List<Point>();
        TargetCurve = new List<Point>();
    }
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual IList<Point> ActualCurve { get; set; }
    public virtual IList<Point> TargetCurve { get; set; }
}

public class Point
{
    public virtual int Id { get; set; }
    public virtual double X { get; set; }
    public virtual double Y { get; set; }
}

public class FooMapping() : ClassMap<Foo>
{
    Table("Foos");
    Id(x => x.Id).GeneratedBy.Identity();
    Map(x => x.Name).Not.Nullable();
    HasMany(x => x.ActualCurve ).Cascade.All();
    HasMany(x => x.TargetCurve ).Cascade.All();
}

public class PointMapping() : ClassMap<Point>
{
    Table("Points");
    Not.LazyLoad();
    Id(x => x.Id).GeneratedBy.Identity();
    Map(x => x.X).Not.Nullable();
    Map(x => x.Y).Not.Nullable();
}

These mappings produce

CREATE TABLE [dbo].[Foos](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar] NOT NULL,
    [RecipeRevisionId] [int] NOT NULL

CREATE TABLE [dbo].[Points](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [X] [float] NOT NULL,
    [Y] [float] NOT NULL,
    [FooId] [int] NOT NULL

Essentially, the problem is that when I am pulling a persisted Foo object back out of the database, the Foo.ActualCurve and Foo.TargetCurve lists both get populated with the combined contents of both lists. There clearly isn't a column that keys which set of points belong to the correct curve within Foo, but I am not sure how to change the mapping to maintain the two distinct sets of Points.

1
Isn't the point of Fluent that you don't have to mapping tables to classes. Why not use the built-in functionality?Ash Burlaczenko

1 Answers

0
votes

I think you will need to specify a separate column for the references:

HasMany(x => x.ActualCurve ).Cascade.All().KeyColumn("ActualCurveId");
HasMany(x => x.TargetCurve ).Cascade.All().KeyColumn("TargetCurveId");

EDIT: Column->KeyColumn