Answered by:
Use of ComplexType attribute

Question
-
User1904516115 posted
What is the use of ComplexType attribute in Entity Framework? Without using ComplexType attribute I am able to use inside any Entity Class?
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication12 { public class Customer { public Customer() { Contact = new ContactInfo(); } public int CustomerID { get; set; } public string Name { get; set; } public ContactInfo Contact { get; set; } } public class ContactInfo { public string Email { get; set; } public string Phone { get; set; } } }
Insert operation also creating Customers table with following column.
CustomerID
Name
Contact_Email
Contact_Phone
Monday, September 2, 2019 1:25 PM
Answers
-
User753101303 posted
The only case I see then is if your complex type meets minimal requirements for an entity (ie you need a property named Id or <className>Id).
I gave this a quick try ie adding an Id property to ContactInfo makes this type an entity (exposing this type through the DbContext is not needed). AFAIK you can't "turn off" a key that follows the default naming convention and then using this attribute is your only option if you really want a complex type and a Contact_Id column instead.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, September 2, 2019 5:59 PM
All replies
-
User753101303 posted
Hi,
It allows to store properties directly inside the parent table (ie you'll have a Contact_Email and a Contact_Phone column as part of your "Customer" table) rather than in its own table.
Edit: and you don't have any fluent API code? Your DbContext doesn't define a collection for this ContactInfo type? Maybe EF is opting for this if not finding a collection for this on the context or no key on the class (though it seems a bit weird to me that it does opt for this without an explicit directive). I would have to try to see which behavior I find...
Monday, September 2, 2019 1:52 PM -
User1904516115 posted
Hi PatriceSc,
I am not using FluentAPI. My DbContext doesn't define a collection for this ContactInfo type. I am doing anything wrong?
Thanks,
VinodMonday, September 2, 2019 2:19 PM -
User753101303 posted
Apparently not, I've done a quick test and found the same behavior here. Until now I just always used this attribute without wondering.
For now the only difference I found is a distinct error message if you try to expose your type through your DbContext. It complains about not finding a key for you entity or it complains about reconfiguring an entity type to a complex type. I would have to further investigate to see if I see other differences (configuring keys or whatever on this kind of type could maybe throw an exception for complex types ???)
Though I usually dislike using an attribute or a configuration settings which doesn't change the default behavior, I believe I'll still use this attribute to make my intent explicit for others and myself when browsing code.
Edit: or do you mean you do want to handle ContactInfo as an entity rather than as a complex type ?
Monday, September 2, 2019 3:07 PM -
User1904516115 posted
My intent is to use the ContactInfo as complex type only. With or without ComplexType attribute I am able to use ContactInfo as complex type. If you find any useful information please let me know why should we use this attribute?(Except to make my intent explicit for others and myself when browsing code as you mentioned.)
Monday, September 2, 2019 4:50 PM -
User753101303 posted
The only case I see then is if your complex type meets minimal requirements for an entity (ie you need a property named Id or <className>Id).
I gave this a quick try ie adding an Id property to ContactInfo makes this type an entity (exposing this type through the DbContext is not needed). AFAIK you can't "turn off" a key that follows the default naming convention and then using this attribute is your only option if you really want a complex type and a Contact_Id column instead.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, September 2, 2019 5:59 PM -
User1904516115 posted
In that case it makes sense. If you find any other useful information please let me know.Monday, September 2, 2019 7:24 PM -
User753101303 posted
Depending on what you need next you could look directly at the source code found at https://github.com/aspnet/EntityFramework6
Monday, September 2, 2019 10:27 PM