Question

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?

Was it helpful?

Solution 2

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top