1

I am using Entity Framework 6.X and a code-first approach.

My requirement is I have tables

  1. User
  2. Product

While entering product I need Creator Name, Modifier Name to keep track and these two should reference User table. Because those who entered/modified should be in a user table.

My model is:

public class User : AuditableEntity
{
    [Key]
    [StringLength(35)]
    public string UserId { get; set; }

    [Required]
    [Column(TypeName = "varchar")]
    public string Name { get; set; }

    public string Address { get; set; }

    [Required]
    public long MobileNo { get; set; }

    [Required]
    public string Password { get; set; }

    public string EmailId { get; set; }

    public string MiscData { get; set; }

    [Required]
    public Role Role { get; set; }

    public virtual ICollection<Product> Products { get; set; }
    public virtual ICollection<Product> Products1 { get; set; }
}

public class Product : AuditableEntity
{
    [Key]
    [StringLength(30)]
    public string Id { get; set; }

    [Required]
    public string ProductName { get; set; }

    public string Description { get; set; }

    [Required]
    [Column(TypeName = "Money")]
    public decimal Amount { get; set; }

    [Required]
    [StringLength(35)]
    public override string CreatedBy
    {
        get
        {
            return base.CreatedBy;
        }
        set
        {
            base.CreatedBy = value;
        }
    }

    [Required]
    [StringLength(35)]
    public override string ModifiedBy
    {
        get
        {
            return base.ModifiedBy;
        }
        set
        {
            base.ModifiedBy = value;
        }
    }

    [ForeignKey("CreatedBy")]
    public User User { get; set; }

    [ForeignKey("ModifiedBy")]
    public User User1 { get; set; }
}

But when executing this it generates Db without error and it creates foreign column on CreatedBy, ModifiedBy columns in Product. However in the products it creates User_UserId, User_UserId1 as extra columns.

I am new to Entity Framework.

Please help me.

UPDATE

When i changed the referencing to one column say only CreatedBy that extra column is not coming in products. But when i reference one more column as shown above the problem is coming.

shanmugharaj
  • 3,814
  • 7
  • 42
  • 70
  • Try adding `[InverseProperty("UserId ")]`. See this SO question http://stackoverflow.com/questions/15483019/entity-framework-code-first-how-to-annotate-a-foreign-key-for-a-default-valu – Nikolai Samteladze Jul 28 '15 at 05:20
  • @NikolaiSamteladze Thanks for reply. where i need to add this property ? – shanmugharaj Jul 28 '15 at 05:21

1 Answers1

2

This didn't solve the problem and is left only for historical information. Please see the UPDATE for the solution.

See this SO question. Try adding [InverseProperty("UserId ")] like this:

[InverseProperty("UserId ")]
[ForeignKey("CreatedBy")]
public User User { get; set; }

[InverseProperty("UserId ")]
[ForeignKey("ModifiedBy")]
public User User1 { get; set; }

UDPATE

Also, EF might be confused where to map your User.Products and User.Products1. See this SO question and this MSDN article on code-first relationships. Try changing it to:

[InverseProperty("User")]
public virtual ICollection<Product> Products { get; set; }

[InverseProperty("User1")]
public virtual ICollection<Product> Products1 { get; set; }
Community
  • 1
  • 1
Nikolai Samteladze
  • 7,699
  • 6
  • 44
  • 70
  • I tried like u said its giving me this error **the property must be a valid entity type and the property should have a non-abstract getter and setter** – shanmugharaj Jul 28 '15 at 05:32
  • Thanks man really u saved my time. I was not able to get it since i am very new. – shanmugharaj Jul 28 '15 at 06:09