• Entity Framework 复杂类型


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

    复制代码
     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; }
        }
    复制代码

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

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

    复制代码
    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; }
        }
    复制代码

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

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

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

    如果我的文章对你有帮助,就点一下推荐吧.(*^__^*)
  • 相关阅读:
    20201215王馨瑶 实验一《Python程序设计》实验报告
    20201215第十六周学习总结
    python笔记
    信导笔记
    成绩调节
    2020-2021-1 20201226 《信息安全专业导论》第十三周学习总结
    链表(补交)
    2020-2021-1 20201226 《信息安全专业导论》第十二周学习总结
    Wireshark 实践
    ssh
  • 原文地址:https://www.cnblogs.com/Alex80/p/5496775.html
Copyright © 2020-2023  润新知