class Program { static void Main(string[] args) { using (ContactContext db = new ContactContext()) { List<ContactInformation> conInformList= new List<ContactInformation>(); ContactInformation ContactInformation1 = new ContactInformation { Another = "8", ContactId = 0, Email = "8", Id = 0, Phone = "8", Skype = "89", }; conInformList.Add(ContactInformation1); //ContactInformation1.CreareContactInformation(Contact1); Contact Contact1 = new Contact { Id = 0, Name = "Вася", SurName = "Пупкин", MiddleName = "Николаевич", BirthDate = "1.11.1990", Organization = "Купол", Position = "programmer", ContInforms = conInformList, }; // добавляем их в бд db.Contacts.Add(Con //Contact contact2 = db.Contacts // .Where(o => o.Id == 1) // .FirstOrDefault(); //db.Contacts.Remove(contact2); tact1); // db.SaveChanges(); Console.WriteLine("Объекты успешно сохранены"); // получаем объекты из бд и выводим на консоль var users = db.Contacts; Console.WriteLine("Список объектов:"); foreach (Contact c in users) { Console.WriteLine("{0}.{1} - {2} - {3} - {4} - {5} - {6} - {7}", c.Id, c.Name, c.SurName, c.MiddleName, c.Organization, c.Position, c.BirthDate, c.ContInforms); } } Console.Read(); } } public class ContactInformation { public int ContactId { get; set; } public int Id { get; set; } public string Phone { get; set; } public string Email { get; set; } public string Skype { get; set; } public string Another { get; set; } public virtual Contact Contact {get;set;} } public class Contact { [Key] public int Id { get; set; } public string SurName { get; set; } public string Name { get; set; } public string MiddleName {get;set;} public string BirthDate {get;set;} public string Organization {get;set;} public string Position {get; set;} public virtual ICollection<ContactInformation> ContInforms { get; set; } } public ContactContext() :base("MyBase") { } // Это свойство ссылается на таблицу в базе данных public DbSet<Contact> Contacts { get; set; } public DbSet<ContactInformation> ContInform { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<ContactInformation>().HasRequired(p => p.Contact) .WithMany(b => b.ContInforms) .HasForeignKey(p => p.ContactId); modelBuilder.Ignore<ContactInformation>(); }
- Uh, what are you talking about? You have a ContactInformation collection, you didn’t even try to expand it. - Monk
- @Monk That is, How? - SVD102
- foreach (Contact c in users) {Console.WriteLine ("{0}. {1} - {2} - {3} - {4} - {5} - {6}", c.Id, c.Name, c.SurName, c.MiddleName, c.Organization, c.Position, c.BirthDate); foreach (var s in c.ContInforms) Console.WriteLine ("- {0}. {1} - {2} - {3} - {4}", s.Id, s.Phone, s.Email, s. Skype, s.Another); } - Monk
|