none
how to map List(Of T) with EF4.2 RRS feed

  • Question

  • class A

    public property Id

    public property items as List( Of B)

    end Class

    class B

    public property Id

    pulic property something

    end class

    how can a map the list with EF4.2

    I always have a multiplicity exception.

    • Moved by Paul Zhou Friday, January 20, 2012 5:28 AM move for better support (From:.NET Base Class Library)
    Thursday, January 19, 2012 3:28 PM

Answers

  • Hi Peirens Bart,

    Welcome to MSDN Forum.

    Do you have the database which contains the two tables already? If you have already have the database, please use database-first. Here's a tutorial which can help you to do that, please refer here. If you want to use code-first to do that. Here's the below.

    class Program
        {
            static void Main(string[] args)
            {
               
                using (myContext context = new myContext())
                { 
                    B b = new B();
                    b.Name = "bbb";
                    A a = new A();
                    a.Name = "aaa";
                    List<B> bList = new List<B>();
                    bList.Add(b);
                    a.bList = bList;
                    context.As.Add(a);
                    context.SaveChanges();
                }
            }
        }
    
        public class A
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public List<B> bList { get; set; }
        }
    
        public class B
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public A a { get; set; }
        }
    
        public class myContext : DbContext
        {
            public DbSet<A> As { get; set; }
            public DbSet<B> Bs { get; set; }
        }
    

    Here's a tutorial of code-first, you can also refer to it.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Monday, January 23, 2012 3:22 AM
    Moderator

All replies

  • I'm moving this thread to ADO.NET Entity Framework and LINQ to Entities forum to get better support.

    Have a nice day.


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Friday, January 20, 2012 5:28 AM
  • Hi Peirens Bart,

    Welcome to MSDN Forum.

    Do you have the database which contains the two tables already? If you have already have the database, please use database-first. Here's a tutorial which can help you to do that, please refer here. If you want to use code-first to do that. Here's the below.

    class Program
        {
            static void Main(string[] args)
            {
               
                using (myContext context = new myContext())
                { 
                    B b = new B();
                    b.Name = "bbb";
                    A a = new A();
                    a.Name = "aaa";
                    List<B> bList = new List<B>();
                    bList.Add(b);
                    a.bList = bList;
                    context.As.Add(a);
                    context.SaveChanges();
                }
            }
        }
    
        public class A
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public List<B> bList { get; set; }
        }
    
        public class B
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public A a { get; set; }
        }
    
        public class myContext : DbContext
        {
            public DbSet<A> As { get; set; }
            public DbSet<B> Bs { get; set; }
        }
    

    Here's a tutorial of code-first, you can also refer to it.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us
    Monday, January 23, 2012 3:22 AM
    Moderator