views:

169

answers:

3

I have a Linq-To-Sql mapping between POCO classes and my DB. I want to be able to add properties on my classes which represent slightly more complex constructs than simple scalr values.

For instance, I have a custom struct type which holds two simple scalar values. I don't want to make this another table and then add a FK as the property. Is it possible to map this struct's properties using the Linq-to-Sql XML mapping file?

For instance,

public class Foo
{
    public string Blah { get; set; }
    public Bar Yada { get; set; }
}

public struct Bar
{
    public int A { get; set; }
    public int B { get; set; }
}

How would I specify that Blah, Yada.A, and Yada.B are all persisted as columns on the Foo table?

Can this even be done?

+1  A: 

I think Entity Framework in .NET4 is required to get this done. See this blog post on complex types and entities.

Sander Rijken
Thanks for the tip. I'm really hoping to stick with LINQ-to-SQL for this small app. If I jumped to EF, I'd really have to step back and look at NHibernate and others too.
McKAMEY
A: 

Linq-to-SQL is unborn.

Ciper
Microsoft does not support Linq-to-Sql any more; the only choice is to migrate everthing to EF.Thanks for the points all the same.
Ciper
On the contrary Microsoft still supports Linq-to-Sql and as a matter of fact this site uses it.
JeremySpouken
+1  A: 

This is really lame, but sounds like it might need to be what I have to do if I stick with L2S.

public class Foo
{
    public string Blah { get; set; }

    public int Bar_A { get; set; }
    public int Bar_B { get; set; }

    /* not mapped to DB */
    public Bar Yada
    {
        get { return new Bar { A = this.Bar_A, B = this.Bar_B }; }
        set { this.Bar_A = value.A; this.Bar_B = value.B; }
    }
}

public struct Bar
{
    public int A { get; set; }
    public int B { get; set; }
}

Of course, in the real code, I'd probably make it such that it wasn't creating the value over and over because my real Bar type has more to it than simply containing the values.

McKAMEY