none
Need hep on code first. RRS feed

  • Question

  • Hi all, i'm using EF 4.3. And Code First is really useful. However, i'm facing some problem which i can't figure out a solution.

    I have a class OpCode and a class name SubOpCode as follows.

    Basically, I have different set of OpCode and different set of SubOpCode. 

    An OpCode contains a collection of SubCode associated with it.

    Different OpCode can have the same set of same SubOpCode.

    How can i achieve in code first? Many thanks!


    public class SubOpCode
    {
        public int Id { get; set; }
        public String CodeName { get; set; }
        public OpCode ParentOpCode { get; set }
    }
    
    public class OpCode
    {
        public int Id { get; set;}
        public String CodeName { get; set; }
        public ICollection<SubOpCode> { get; }
    }


    Friday, February 24, 2012 8:48 AM

Answers

  • Hi j3r;

    It sounds like what you need is a Many to Many relationship. If you construct your model as follows it will do that.

    public class SubOpCode
    {
        public int SubOpCodeId { get; set; }
        public String CodeName { get; set; }
    
        public virtual ICollection<OpCode> OpCodes { get; set; }
    }
    
    public class OpCode
    {
        public int OpCodeId { get; set;}
        public String CodeName { get; set; }
    
        public virtual ICollection<SubOpCode> SubOpCodes { get; set; }
    }
    
    public class Context : DbContext
    {
        public DbSet<OpCode> OpCodes { get; set; }
        public DbSet<SubOpCode> SubOpCodes { get; set; }
    }

    In the database it will look like this:

    Many to Many Relationship

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".


    • Edited by Fernando Soto - MCSD Friday, February 24, 2012 4:56 PM
    • Marked as answer by j3r Monday, February 27, 2012 1:47 AM
    Friday, February 24, 2012 4:55 PM
  • Hi j3r;

    One omission from my first solution was that I left out the class constructors. Add the following constructors to the classes.

    // For the SubOpCode
    public SubOpCode()
    {
        OpCodes = new Collection<OpCode>();    
    }
    
    // For the OpCode
    public OpCode( )
    {
        SubOpCodes = new Collection<SubOpCode>( );    
    }

    To add records to the database you can do the following.

    OpCode oc = new OpCode();
    oc.CodeName = "Code 1";
    SubOpCode soc = new SubOpCode();
    soc.CodeName = "code 1";
    oc.SubOpCodes.Add(soc);
    
    // ctx is an instance of the DbContext
    ctx.OpCodes.Add( oc );
    ctx.SaveChanges( );

    After doing the above a record will be added to all three tables.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by j3r Tuesday, February 28, 2012 1:03 AM
    Monday, February 27, 2012 2:51 PM

All replies

  • Hi j3r;

    It sounds like what you need is a Many to Many relationship. If you construct your model as follows it will do that.

    public class SubOpCode
    {
        public int SubOpCodeId { get; set; }
        public String CodeName { get; set; }
    
        public virtual ICollection<OpCode> OpCodes { get; set; }
    }
    
    public class OpCode
    {
        public int OpCodeId { get; set;}
        public String CodeName { get; set; }
    
        public virtual ICollection<SubOpCode> SubOpCodes { get; set; }
    }
    
    public class Context : DbContext
    {
        public DbSet<OpCode> OpCodes { get; set; }
        public DbSet<SubOpCode> SubOpCodes { get; set; }
    }

    In the database it will look like this:

    Many to Many Relationship

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".


    • Edited by Fernando Soto - MCSD Friday, February 24, 2012 4:56 PM
    • Marked as answer by j3r Monday, February 27, 2012 1:47 AM
    Friday, February 24, 2012 4:55 PM
  • Thanks for the help!! Another question..... hehe. How do i mark a field to be unique?

    For example for class OpCode. I wan Id to retain and having CodeName to be unique for every row.

    public class OpCode
    {
        [Key]
        public int OpCodeId { get; set; }
    
        // How do i mark it to be unique?
        public String CodeName { get; set; }
    
        public virtual ICollection<OpCode> OpCodes { get; set; }
    }


    Monday, February 27, 2012 1:53 AM
  • I'm facing some problems inserting data.

    OpCode.Name and SubCode.Name should be unique for every entry and the Auto-generated table SubOpCodeOpCodesshould be updated accordingly.

    I don't think code first has this feature yet. It's pretty hard for me to Add data.

    I tried using LINQ to check for OpCode and SubOpCode if Name exist. But i don't how to insert data accordingly.


    Monday, February 27, 2012 7:09 AM
  • Hi j3r;

    The unique constrant is not yet supported by Entity Framework 4.0 - 4.3, You can have a look at this post on Stack Overflow for a possible solution.

    Unique Constraint in Entity Framework Code First

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Monday, February 27, 2012 2:17 PM
  • Hi j3r;

    One omission from my first solution was that I left out the class constructors. Add the following constructors to the classes.

    // For the SubOpCode
    public SubOpCode()
    {
        OpCodes = new Collection<OpCode>();    
    }
    
    // For the OpCode
    public OpCode( )
    {
        SubOpCodes = new Collection<SubOpCode>( );    
    }

    To add records to the database you can do the following.

    OpCode oc = new OpCode();
    oc.CodeName = "Code 1";
    SubOpCode soc = new SubOpCode();
    soc.CodeName = "code 1";
    oc.SubOpCodes.Add(soc);
    
    // ctx is an instance of the DbContext
    ctx.OpCodes.Add( oc );
    ctx.SaveChanges( );

    After doing the above a record will be added to all three tables.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked as answer by j3r Tuesday, February 28, 2012 1:03 AM
    Monday, February 27, 2012 2:51 PM
  • Thanks for the update. I'm kind of having trouble in the logic part of the code.

    I don't know how i can manually update the m-m relationship.

    public class OperationDbContext : DbContext
    {
        private DbSet<OpCode> OpCodes {get; set; 
        private DbSet<SubOpCode> OpCodes {get; set; 
    
        public void AddOpcode(Opcode _opCode)
        {
            /*
                if opCode exist
                {
                    if subCode don't exist
                    {
                        Insert SubOpCode and update m-m relationship
                    }
                }
                else opCode don't exist
                {
                    Insert OpCode
                    
                    if subCode don't exist
                    {
                        Insert SubOpCode and update m-m relationship
                    }
                }
            */
    
            this.SaveChanges();
        }
    }

    If it's not possible, probably i will create my m-m and do the LINQ manually. What do you think?

    public class OpCode
    {
        public int Id { get; set; }
        public String Name { get; set; }
    }
    
    public class SubOpCode
    {
        public int Id { get; set; }
        public String Name { get; set; }
    }
    
    public class OpCodeSubOpCode
    {
        public OpCode OpCodeId { get; set; }
        public SubOpCode SubOpCodeId { get; set; }
    }
    
    public class OperationDbContext : DbContext
    {
        public DbSet<OpCode> { get; set; }
        public DbSet<SubOpCode> { get; set; }
        public DbSet<OpCodeSubOpCode> { get; set; }
    }
    
    

    Tuesday, February 28, 2012 1:15 AM
  • Hi j3r;

    I am not an expert on Windows Form / WPF and the use of IBindingList and seeming this is a different question then the original question of this thread can you post a new question in the correct form for help with this new question.

    Thanks;


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Tuesday, February 28, 2012 3:36 PM
  • No worries. Many thanks for the help!!
    Wednesday, February 29, 2012 12:07 AM