To illustrate what a complex attribute is, let's give an example.
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 ;}}
There are two classes: company address and home address. They have four identical attributes: streetaddress, city, state, and zipcode. Structure mapped to the database
Here, we can combine these four attributes into a complex attribute address. The modified class is:
Public class companyaddress {public int ID {Get; set;} Public String companyName {Get; set;} Public Address {Get; Set ;}} public class familyaddress {public int ID {Get; set;} Public 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 ;}}
The generated database
We can see that the two tables still have the corresponding address attribute information. The address class in the Code is a complex attribute, which is not mapped to the corresponding table in the database, but our code is much simpler.
Therefore, if several attributes are useful in several classes, you can set these attributes into a complex type and add the attribute of this complex type to the corresponding class.
Entity Framework complex types