Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF-Code First navigation property foreign key in complex type

I have complex type for Audit fields

My complex type:

[ComplexType]
public class AuditData  {
    [Column("CreatorUserId")]
    public int? CreatorUserId { get; set; }
    public DateTime? CreationTime { get; set; }
    [Column("ModifierUserId")]
    public int? ModifierUserId { get; set; }
    public DateTime? ModificationTime { get; set; }
}

My base Entity (all other inherti this one) has AuditData property:

public abstract class Entity : IEntity, IAuditedEntity, INotifiedDbContextBeforeSave
{

    // Summary:
    //     Unique identifier for this entity.
    public int Id { get; set; }
    public bool IsActive { get; set; }
    public int Old_Id { get; set; }
    public string Old_TableName { get; set; }        
    [Timestamp]
    public byte[] RowVersion { get; set; }        
    public AuditData AuditData { get; set; }
    // can this 2 lines below work as navigation property with foreign key in complex type
    public virtual User CreatorUser { get; set; }
    public virtual User ModifierUser { get; set; }

    //... other fields
}

I have 2 navigation properties CreatorUser and ModifierUser. I know you cant have navigation property in ComplexType but can my navigation property on entity be mapped with foreign key in complexType

something like:

    [ForeignKey("CreatorUserId")] // --> should point to AuditData.CreatorUserId
    public virtual User CreatorUser { get; set; }

becouse CreatorUserId will be property in every entity but EF is not aware of it. Mybe there is solution in fluent API ?

like image 615
Matija Gluhak Avatar asked Oct 11 '15 13:10

Matija Gluhak


People also ask

How do you set a foreign key in code first approach?

To create Foreign Key, you need to use ForeignKey attribute with specifying the name of the property as parameter. You also need to specify the name of the table which is going to participate in relationship. I mean to say, define the foreign key table. Thanks for reading this article, hope you enjoyed it.

How do I use navigation property in Entity Framework?

A navigation property is an optional property on an entity type that allows for navigation from one end of an association to the other end. Unlike other properties, navigation properties do not carry data. A navigation property definition includes the following: A name.

What is navigation in code first approach?

What is the Navigation Property? It is a way to represent a foreign key relationship in the database or define the relationship between the two entities.


1 Answers

The official documentation says:

Complex types are non-scalar 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. Because complex types do not have keys, complex type objects cannot be managed by the Entity Framework apart from the parent object.

It follows that that complex types can not participate in any relations among entities, so they can't contain foreign keys

like image 108
Lukas Kabrt Avatar answered Oct 23 '22 13:10

Lukas Kabrt