I have two entities – UserObj and Producer.
UserObj stores user-related profile data
Producer is a company that a userobj can be associated with. To associate these two I wanted to make an entity that lets me access the data of each from one entity.
I’m trying to link the two – but I’m not quite sure how to do it and I bet it is something small. Thanks for any and all help!
ProducerUser
public class ProducerUser
{
[ForeignKey("UserObj")]
public Guid UserObjID { get; set; }
[ForeignKey("Producer")]
public int ProducerID { get; set; }
public virtual UserObj UserObj { get; set; }
public virtual Producer Producer { get; set; }
}
UserObj
public class UserObj : Contact
{
[Key]
//public override string Email { get; set; }
public Guid UserObjID { get; set; }
[Display(Name = "First Name")]
// [Required]
public string FirstName { get; set; }
// [Required]
[Display(Name = "Last Name")]
public string LastName { get; set; }
public int UserTypeID { get; set; }
public virtual UserType UserType { get; set; }
public static UserObj GetUserObj(string GUID, vfContext db)
{
Guid guidUser = new Guid(GUID);
return db.UserObjs.Find(guidUser);
}
public string GetFullName()
{
return this.FirstName + " " + this.LastName;
}
}
Producer
public class Producer : Contact
{
[Key]
public int ProducerID { get; set; }
public string Name { get; set; }
public string Twitter { get; set; }
public virtual ICollection<Wine> Wines { get; set; }
}
To establish a many to many relationship add ICollection<> on both sides of the relationship. In your case:
To the class Producer add
To the class UserObj add
After that you can remove the declaration of the class ProducerUser. EF will generate it for you.