首页 > 其他 > 详细

Entity Framework 复杂类型(转)

时间:2014-01-25 18:47:07      阅读:280      评论:0      收藏:0      [点我收藏+]

为了说明什么是复杂属性,先举一个例子。

bubuko.com,布布扣
bubuko.com,布布扣
 public class CompanyAddress
    {
        public int ID { get; set; }
        public string CompanyName { get; set; }
        public string StreetAddress { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string ZipCode { get; set; }
    }

    public class FamilyAddress
    {
        public int ID { get; set; }
        public string StreetAddress { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string ZipCode { get; set; }
    }
bubuko.com,布布扣
bubuko.com,布布扣

上面有两个类:公司地址和家庭地址,它们有四个相同的属性:StreetAddress、City、State、ZipCode。映射到数据库中的结构如图:

bubuko.com,布布扣

这里,我们可以将这四个属性集合成一个复杂属性Address,修改后的类为:

bubuko.com,布布扣
bubuko.com,布布扣
public class CompanyAddress
    {
        public int ID { get; set; }
        public string CompanyName { get; set; }
        public Address Address { get; set; }
    }

    public class FamilyAddress
    {
        public int ID { get; set; }
        public Address Address { get; set; }
    }

    [ComplexType]
    public class Address
    {
        public string StreetAddress { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string ZipCode { get; set; }
    }
bubuko.com,布布扣
bubuko.com,布布扣

此时,所生成的数据库如图:

bubuko.com,布布扣

可以看到,两张表中仍然具有相应的地址属性信息。代码中的Address类就是复杂属性,它并不会在数据库中映射成相应的表,但我们的代码确简洁了许多。

所以如果有几个属性在几个类中都有用到,那么就可以将这几个属性集合成一个复杂类型,并在相应的类中增加这个复杂类型的属性。

转自:http://www.cnblogs.com/Gyoung/archive/2013/01/17/2864747.html

Entity Framework 复杂类型(转)

原文:http://www.cnblogs.com/ITGirl00/p/3533219.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!