代码之家  ›  专栏  ›  技术社区  ›  mckamey

如何扁平LINQ到SQL表的映射?

  •  1
  • mckamey  · 技术社区  · 15 年前

    我在poco类和db之间有一个linq-to-sql映射。我希望能够在类上添加属性,这些属性表示比简单的scaler值稍微复杂一些的构造。

    例如,我有一个自定义结构类型,它包含两个简单的标量值。我不想再创建一个表,然后添加FK作为属性。是否可以使用LINQ to SQL XML映射文件映射此结构的属性?

    例如,

    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; }
    }
    

    我该怎么说呢 Blah , Yada.A Yada.B 都作为列保留在 Foo 桌子?

    甚至可以这样做吗?

    2 回复  |  直到 15 年前
        1
  •  1
  •   Sander Rijken    15 年前

    我认为需要.net4中的实体框架来完成这项工作。见 this blog post 在复杂类型和实体上。

        2
  •  1
  •   mckamey    15 年前

    这真的很蹩脚,但听起来如果我坚持使用L2,可能需要做些什么。

    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; }
    }
    

    当然,在真正的代码中,我可能会使它不会一次又一次地创造价值,因为我的真实 Bar 类型不仅仅包含值,还包含更多内容。