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.
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...
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 ?
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.)
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.
Member
68 Points
240 Posts
Use of ComplexType attribute
Sep 02, 2019 01:25 PM|vinodkpasi|LINK
What is the use of ComplexType attribute in Entity Framework? Without using ComplexType attribute I am able to use inside any Entity Class?
CustomerID
Name
Contact_Email
Contact_Phone
All-Star
48500 Points
18071 Posts
Re: Use of ComplexType attribute
Sep 02, 2019 01:52 PM|PatriceSc|LINK
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...
Member
68 Points
240 Posts
Re: Use of ComplexType attribute
Sep 02, 2019 02:19 PM|vinodkpasi|LINK
Hi PatriceSc,
I am not using FluentAPI. My DbContext doesn't define a collection for this ContactInfo type. I am doing anything wrong?
Thanks,
Vinod
All-Star
48500 Points
18071 Posts
Re: Use of ComplexType attribute
Sep 02, 2019 03:07 PM|PatriceSc|LINK
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 ?
Member
68 Points
240 Posts
Re: Use of ComplexType attribute
Sep 02, 2019 04:50 PM|vinodkpasi|LINK
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.)
All-Star
48500 Points
18071 Posts
Re: Use of ComplexType attribute
Sep 02, 2019 05:59 PM|PatriceSc|LINK
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.
Member
68 Points
240 Posts
Re: Use of ComplexType attribute
Sep 02, 2019 07:24 PM|vinodkpasi|LINK
All-Star
48500 Points
18071 Posts
Re: Use of ComplexType attribute
Sep 02, 2019 10:27 PM|PatriceSc|LINK
Depending on what you need next you could look directly at the source code found at https://github.com/aspnet/EntityFramework6