none
How do you create a model with a lookup item using code first? RRS feed

  • Question

  • I have a model that will contain a lookup item that exists in another table.  My problem is that when I try to create the main object that uses the lookup item, it attempts to create a new object and throws an error because the primary key is already in the system.

    Code example:

    public class StoreDepartment
     {
         [Key]
         public int ID { get; set; }
         public int StoreID { get; set; }
         public int DepartmentID { get; set; }
     
         [ForeignKey("DepartmentID")]
         public Department Department { get; set; }
         public string ShelvingLinealFT { get; set; }
    }

    public class Department
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int DepartmentNumber { get; set; }
        public string DepartmentName { get; set; }
        public bool InActive { get; set; }
    }

    I know that this creates a one to one relationship from StoreDepartment to Department.  What is the proper syntax to get this done correctly?

     
    Friday, January 25, 2013 6:54 PM

All replies

  • Just to be clear.  I need to have a list a Departments that users can choose from.  This is my lookup list.  Users will be selecting one and will add respective info for that department.  The main object is a store and it will have a collection of store departments.  Hope this helps...
    Friday, January 25, 2013 6:59 PM
  • Hi Tuff,

    Welcome to the MSDN forum.

    Do you mean the relationship for StoreDepartment and Department is one to one, and the relationship for Store and StoreDepartment is one to many?

    Could you please show us the code snippet about creating new objects and error occurred?

    Have a nice day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, January 28, 2013 3:26 AM
  • Ok.. been busy with other emergencies.  Let's start from scratch on what I'm trying to accomplish.

    I'd like to have a table called Department.  This table will hold all of the Department information such as Name, DepartmentNumber, Inactive(flag), etc.

    Within my Store object, I need to store the amount of space each department will be using.  Each store can have different departments.

    To solve this, I tried to create an ICollection of StoreDepartment. 

    What's the best method of setting up something like this?

    Wednesday, February 6, 2013 12:55 AM
  • Should the relationship between Department and StoreDepartment be 1-to-1 or 1-to-many?

    My initial reaction to this was that StoreDepartment and Department should be merged into the same class, but your description kind of sounds like the relationship is really 1-to-many where StoreDepartment is the join table between the Many-to-Many relationship between store and department. The question really is whether or not a department ever belongs to more than one store?


    We are seeing a lot of great Entity Framework questions (and answers) from the community on Stack Overflow. As a result, our team is going to spend more time reading and answering questions posted on Stack Overflow. We would encourage you to post questions on Stack Overflow using the entity-framework tag. We will also continue to monitor the Entity Framework forum.

    Thursday, February 14, 2013 12:20 AM
    Moderator
  • The relationship between Department and StoreDepartment is one to many.  As for data, here's what I'm trying to accomplish:

    I have a list of departments that I will be using which have a department number and name. Ex:

    Sample Record 1: 

    • DepartmentNumber = 26
    • DepartmentName = Baked Goods

    Sample Record 2:

    • DepartmentNumber = 75
    • DepartmentName = Canned Goods

    I'm trying to capture what shelving requirements are setup per store by department and will need to have a list of managed current departments.   Am I approaching this correctly?

    Monday, April 8, 2013 6:26 PM