1

i have a base Entity Class like this :

 public abstract class BaseEntity
{
    [System.ComponentModel.DataAnnotations.Key]
    [System.ComponentModel.DataAnnotations.Required]
    [Column("ID")]
    [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)]
    [System.ComponentModel.DataAnnotations.DataType("bigint")]
    public long id { get; set; }

    public virtual User createdBy { get; set; }

    public virtual User updatedBy { get; set; }
    public DateTime createdDate { get; set; }
    public DateTime updatedDate { get; set; }
}

and a User Class for example like this :

public class User:BaseEntity
{
    public string userName { get; set; }


}

but the EF give me an error when i want add record to the user table . the error is this :

Unable to determine the principal end of an association between the types 'MvcPractice.Models.User' and 'MvcPractice.Models.User'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.

ocuenca
  • 38,548
  • 11
  • 89
  • 102
Mohammad Mirzaeyan
  • 845
  • 3
  • 11
  • 30
  • Are you missing a [foreign key](https://msdn.microsoft.com/en-au/data/gg193958.aspx) – lloyd May 21 '15 at 12:54
  • i have to add foreign key in BaseEntity class like this ? [ForiegnKey] public virtual User createdBy { get; set; } – Mohammad Mirzaeyan May 21 '15 at 13:36
  • [OnModelCreating](http://www.codeproject.com/Articles/206410/How-to-Configure-a-Self-Referencing-Entity-in-Code) add in the self references – lloyd May 21 '15 at 13:52
  • i get this now : Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values. – Mohammad Mirzaeyan May 21 '15 at 14:09
  • not sure, post a new question, maybe [this](http://weblogs.asp.net/manavi/associations-in-ef-4-1-code-first-part-5-one-to-one-foreign-key-associations) adapted to a [one to many](http://dotnet.dzone.com/news/using-self-referencing-tables) – lloyd May 21 '15 at 15:31
  • You are missing a foreign key. Check out [this answer](http://stackoverflow.com/questions/6531671/what-does-principal-end-of-an-association-means-in-11-relationship-in-entity-fr). – flaudre May 23 '15 at 15:40
  • using foreig key on wich field ? remember that base entity may be use in another model – Mohammad Mirzaeyan May 24 '15 at 16:31

0 Answers0