Anda di halaman 1dari 5

20/05/2015

EntityFrameworkComplexTypesEFDesigner

Data Developer Center


Home

Library

Documentation

Learn

Videos

SearchDatawithBing

Downloads

Articles

Books

Support

Handson Labs

Community

United States English

Sign in

Forums

Webcasts

Data Developer Center > Learn > Entity Framework > Get Started > Complex Types EF Designer

Complex Types in the EF Designer


This topic shows how to map complex types with theEntity Framework Designer EF Designer and how to query for entities that contain properties of complex
type.
The following image shows the main windows that are used when working with the EF Designer.

Note:When you build the conceptual model, warnings about unmapped entities and associations may appear in the Error List. You can ignore these warnings because
after you choose to generate the database from the model, the errors will go away.

This page covers the following topics.


What is a Complex Type
Refactor an Entitys Properties into New Complex Type
Create a New Complex Type
Add Properties to a Complex Type
Rename a Complex Type
Add an Existing Complex Type to an Entity and Map its Properties to Table Columns
Map a Function Import to a Complex Type
Delete a Complex Type
Query for Entities Containing Properties of Complex Type

What is a Complex Type


Complex types are nonscalar properties of entity types that enable scalar properties to be organized within entities. Like entities, complex types consist of scalar
properties or other complex type properties.
When you work with objects that represent complex types, be aware of the following:
Complex types do not have keys and therefore cannot exist independently. Complex types can only exist as properties of entity types or other complex types.
Complex types cannotparticipate in associations and cannot contain navigation properties.
Complex type properties cannot benull. An InvalidOperationExceptionoccurs when DbContext.SaveChangesis called and a null complex object is
encountered. Scalar properties of complex objects can benull.
Complex types cannot inherit from other complex types.
You must define the complex type as aclass.
EF detects changes to members on a complex type object when DbContext.DetectChanges is called. Entity Framework calls DetectChanges automatically
when the following members are called: DbSet.Find, DbSet.Local, DbSet.Remove, DbSet.Add, DbSet.Attach, DbContext.SaveChanges,
DbContext.GetValidationErrors, DbContext.Entry, DbChangeTracker.Entries.

Refactor an Entitys Properties into New Complex Type


If you already have an entity in your conceptual model you may want to refactor some of the properties into a complex type property.

https://msdn.microsoft.com/enus/data/jj680147.aspx

1/5

20/05/2015

EntityFrameworkComplexTypesEFDesigner

On the designer surface, select one or more properties excluding navigation properties of an entity, then rightclick and selectRefactor > Move to New
Complex Type.

A new complex type with the selected properties is added to the Model Browser. The complex type is given a default name.
A complex property of the newly created type replaces the selected properties. All property mappings are preserved.

Create a New Complex Type


You can also create a new complex type that does not contain properties of an existing entity.
Rightclick the Complex Types folder in the Model Browser, point toAddNew Complex Type. Alternatively, you can select the Complex Types folder and press
theInsertkey on your keyboard.

A new complex type is added to the folder with a default name. You can now add properties to the type.

Add Properties to a Complex Type


Properties of a complex type can be scalar types or existing complex types. However, complex type properties cannot have circular references. For example, a
complex typeOnsiteCourseDetailscannot have a property of complex typeOnsiteCourseDetails.
You can add a property to a complex type in any of the ways listed below.
Rightclick a complex type in the Model Browser, point toAdd, then point toScalar PropertyorComplex Property, then select the desired property type.
Alternatively, you can select a complex type and then press theInsertkey on your keyboard.

https://msdn.microsoft.com/enus/data/jj680147.aspx

2/5

20/05/2015

EntityFrameworkComplexTypesEFDesigner

A new property is added to the complex type with a default name.


OR
Rightclick an entity property on the EF Designer surface and selectCopy, then rightclick the complex type in the Model Browser and selectPaste.

Rename a Complex Type


When you rename a complex type, all references to the type are updated throughout the project.
Slowly doubleclick a complex type in the Model Browser.
The name will be selected and in edit mode.
OR
Rightclick a complex type in the Model Browser and selectRename.
OR
Select a complex type in the Model Browser and press the F2 key.
OR
Rightclick a complex type in the Model Browser and selectProperties. Edit the name in thePropertieswindow.

Add an Existing Complex Type to an Entity and Map its Properties to Table Columns
1. Rightclick an entity, point toAdd New, and selectComplex Property.
A complex type property with a default name is added to the entity. A default type chosen from the existing complex types is assigned to the property.
2. Assign the desired type to the property in thePropertieswindow.
After adding a complex type property to an entity, you must map its properties to table columns.
3. Rightclick an entity type on the design surface or in theModel Browserand selectTable Mappings.
The table mappings are displayed in theMapping Detailswindow.
4. Expand theMaps to <Table Name>node.
AColumn Mappingsnode appears.
5. Expand theColumn Mappingsnode.
A list of all the columns in the table appears. The default properties if any to which the columns map are listed under theValue/Propertyheading.
6. Select the column you want to map, and then rightclick the correspondingValue/Propertyfield.
A dropdown list of all the scalar properties is displayed.
7. Select the appropriate property.

8. Repeat steps 6 and 7 for each table column.

https://msdn.microsoft.com/enus/data/jj680147.aspx

3/5

20/05/2015

EntityFrameworkComplexTypesEFDesigner

Note:To delete a column mapping, select the column that you want to map, and then click theValue/Propertyfield. Then, select Delete from the dropdown list.

Map a Function Import to a Complex Type


Function imports are based on stored procedures. To map a function import to a complex type, the columns returned by the corresponding stored procedure must
match the properties of the complex type in number and must have storage types that are compatible with the property types.
Doubleclick on an imported function that you want to map to a complex type.

Fill in the settings for the new function import, as follows:


Specify the stored procedure for which you are creating a function import in theStored Procedure Namefield. This field is a dropdown list that displays
all the stored procedures in the storage model.
Specify the name of the function import in theFunction Import Namefield.
SelectComplexas the return type and then specify the specific complex return type by choosing the appropriate type from the dropdown list.

ClickOK.
The function import entry is created in the conceptual model.

Customize Column Mapping for Function Import


Rightclick the function import in the Model Browser and selectFunction Import Mapping.
TheMapping Detailswindow appears and shows the default mapping for the function import. Arrows indicate the mappings between column values and
property values. By default, the column names are assumed to be the same as the complex type's property names. The default column names appear in gray
text.

https://msdn.microsoft.com/enus/data/jj680147.aspx

4/5

20/05/2015

EntityFrameworkComplexTypesEFDesigner

If necessary, change the column names to match the column names that are returned by the stored procedure that corresponds to the function import.

Delete a Complex Type


When you delete a complex type, the type is deleted from the conceptual model and mappings for all instances of the type are deleted. However, references to the
type are not updated. For example, if an entity has a complex type property of type ComplexType1 and ComplexType1 is deleted in the Model Browser, the
corresponding entity property is not updated. The model will not validate because it contains an entity that references a deleted complex type. You can update or
delete references to deleted complex types by using the Entity Designer.
Rightclick a complex type in the Model Browser and selectDelete.
OR
Select a complex type in the Model Browser and press the Delete key on your keyboard.

Query for Entities Containing Properties of Complex Type


The following code shows how to execute a query that returns a collection of entity type objects that contain a complex type property.

using(SchoolEntitiescontext=newSchoolEntities())
{
varcourses=
fromcincontext.OnsiteCourses
orderbyc.Details.Time
selectc

foreach(varcincourses)
{
Console.WriteLine("Time:"+c.Details.Time)
Console.WriteLine("Days:"+c.Details.Days)
Console.WriteLine("Location:"+c.Details.Location)
}
}

2015 Microsoft. All rights reserved. Terms of Use

| Trademarks | Privacy Statement | Site Feedback

https://msdn.microsoft.com/enus/data/jj680147.aspx

5/5

Anda mungkin juga menyukai