zoukankan      html  css  js  c++  java
  • Lerning Entity Framework 6 ------ Complex types


    Complex types are classes that map to a subset of columns of a table.They don't contains key. They are the Value Objects. For example, you have a entity named Company:

    public class Company
    {
        public int CompanyId { get; set; }
    
        public string CompanyName { get; set; }
    
        public string Street { get; set; }
    
        public string City { get; set; }
    
        public string Zip { get; set; }
    }
    

    You can Mondify it like this:

    public class Company
    {
        public Company()
        {
            this.Address = new Address();
        }
        public int CompanyId { get; set; }
    
        public string CompanyName { get; set; }
    
        public Address Address { get; set; }
    }
    
    public class Address
    {
        public string Street { get; set; }
    
        public string City { get; set; }
    
        public string Zip { get; set; }
    }
    

    This structure can made the entity's meaning clearer than before. and the class Address can be used as a common value object for many classes. Now we complete all codes:

    public class Company
    {
        public Company()
        {
            this.Address = new Address();
        }
        public int CompanyId { get; set; }
    
        [MaxLength(100)]
        public string CompanyName { get; set; }
    
        public Address Address { get; set; }
    }
    
    public class Person
    {
        public Person()
        {
            this.Address = new Address();
        }
        public int PersonId { get; set; }
    
        [MaxLength(50)]
        public string PersonName { get; set; }
    
        public Address Address { get; set; }
    }
    
    public class Address
    {
        [MaxLength(100)]
        public string Street { get; set; }
    
        [MaxLength(50)]
        public string City { get; set; }
    
        [MaxLength(10)]
        public string Zip { get; set; }
    }
    
    public class MyContext:DbContext
    {
        public MyContext() : base("name=Test")
        {
    
        }
    
        public DbSet<Company> Companies { get; set; }
    
        public DbSet<Person> People { get; set; }
    }
    

    Then, execute following commands in NuGet command line:

    • Enable-Migrations
    • Add-Migration Init
    • Update-Database

    Now, The table people and companies'structures like this:

    图片.png-8.6kB

    图片.png-8.9kB

    Let's have a test:

    static void Main(string[] args)
    {
        Address address = new Address
        {
            City = "BeiJing",
            Street = "ChangAn Street",
            Zip = "119"
        };
    
        Company company = new Company
        {
            CompanyName = "TianAnMen",
            Address = address
        };
    
        Person person = new Person
        {
            PersonName = "伟人",
            Address = address
        };
    
        using (MyContext db = new MyContext())
        {
            db.Companies.Add(company);
            db.People.Add(person);
    
            db.SaveChanges();
        }
    }
    

    You can provide the configuration for complex types. We can do so by providing a configuration class . The only difference is that we use a different base class, ComplexTypeConfiguration, not EntityTypeConfiguration.

    That's all.

  • 相关阅读:
    Unity NGUI 2D场景添加按钮
    EaseType缓动函数
    在没有网络的情况下用安卓手机和数据线让台式电脑上网
    面向对象编程
    static与C#中的static
    C#基础
    iSensor APP 之 摄像头调试 OV5642
    iSensor APP 之 摄像头调试 OV9655
    USB3.0之高速视频传输测试 双目相机(mt9p031、mt9m001)带宽高达300M测试 配合isensor测试 500万像素15fps
    模拟摄像头解码模块最新测试 TVP5150模块 FPGA+SDRAM+TVP5150+VGA 实现PAL AV输入 VGA视频输出
  • 原文地址:https://www.cnblogs.com/zzy0471/p/6859727.html
Copyright © 2011-2022 走看看