0

I need to seed my Data using a one to many relationship ( A contact can have many addresses) But it wont let me seed Addresses that i specified as an ICollection. I cant store it as a string or int. so what do i do?

 namespace SuccessEd.Data.Model
{
    public class Contact
    {
        public int ContactId { get; set; }

        public string FirstName { get; set; }
        public string LastName { get; set; }

        public virtual ICollection<Address> Addresses { get; set; }
    }
}




 namespace SuccessEd.Data.Model
{
    public class Address
    {
        public int AddressId { get; set; }

        public string HomeAddress { get; set; }
        public string BusinessAddress { get; set; }
        public string PoBox { get; set; }

        public int ContactId { get; set; }
        public virtual Contact Contact { get; set; }

    }
Pope43
  • 43
  • 7
  • Can you tell us exactly what does not work as expected? – Isantipov Oct 26 '13 at 17:01
  • oh i just wanted to know if what i did above is correct? I need someone to pint me in the write direction since my school book or the internet isn't really telling me how to do a one to many relationship – Pope43 Oct 26 '13 at 17:05
  • You have a list of contacts and you want a function to return you list of addresses which belong to a contact who has a specific last name? – liran63 Oct 26 '13 at 17:05

3 Answers3

2

I think you are on the right track. Altough i don't think Addresses needs to be virtual. If you are working with a database, it might be a good idea to add a property called contact id and give it to contact and adress.

Robin Dijkhof
  • 18,665
  • 11
  • 65
  • 116
2

I would recommend to use something that called Fluent API. You can read more about Fluent api here

to make things easier you can take a look at this approach: I did some changes to your classes

namespace SuccessEd.Data.Model
{
    public class Contact
    {        
        public Contact () 
        {
            AddressList = new List<Address>();
        }

        public int ContactId { get; set; }

        public string FirstName { get; set; }
        public string LastName { get; set; }

        public virtual ICollection<Address> Addresses { get; set; }
   }
}

and the other one

namespace SuccessEd.Data.Model
{
    public class Address
    {
        public Adress() {}

        public int AddressId { get; set; }

        public string HomeAddress { get; set; }
        public string BusinessAddress { get; set; }
        public string PoBox { get; set; }

        public virtual Contact Contact { get; set; }
    }
}

Remember your classes has to be public. both of them!!

on your OnModelCrating. With this you must give an address to a user

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{

    modelBuilder.Entity<Contact>().HasRequired<Address>(s => s.AddressId).WithMany(s => s.AddressList).HasForeignKey(s => s.ContactId);
}

I hope this will help you solve your problem.

Cheers

matthias krull
  • 4,389
  • 3
  • 34
  • 54
photowalker
  • 353
  • 1
  • 8
  • Haha I'm still rather new to this site. I use tabs all the time and jumps out of the box. this is my second day posting comments/answers so yea it should get better pretty soon :) – photowalker Oct 26 '13 at 17:32
  • Well I appreciate I know enough to understand where you were going and what you are trying to do so I appreciate the example – Pope43 Oct 26 '13 at 17:55
1

You are on the right track except you need to use ICollection<> instead of List<> for addresses in Contact class. Like this:

public virtual ICollection<Address> Addresses { get; set; }

Check this as to why: Why use ICollection and not IEnumerable or List<T> on many-many/one-many relationships?

And more explanation here on MSDN: http://msdn.microsoft.com/en-us/data/jj591620.aspx#UniDirectional

Community
  • 1
  • 1
Vinod Kumar Y S
  • 628
  • 3
  • 9