Entity Framework / таблица отношений - PullRequest
0 голосов
/ 07 апреля 2020

У меня есть таблица, в которой зарегистрированы все поля, например, в моей таблице «Id_Cus» является первичным ключом. Как правильно указать, что в таблице 2 "Id_Cus" был взят из таблицы 1? Я прикрепляю скриншоты

Я прикрепил классы, в которых зарегистрированы свойства поля. Однако в первой таблице реализация этих полей встроена в интерфейс. Скажите, где именно я должен указать, что «Id_Cus» во второй таблице следует взять из первой таблицы (в самом классе, как на скриншоте или в интерфейсе) и как это сделать?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using MySql.Data;
using System.Data.Entity;

using WcfRestFullService.Model;

namespace WcfRestFullService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "CustomerSevice" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select CustomerSevice.svc or CustomerSevice.svc.cs at the Solution Explorer and start debugging.
    public class CustomerSevice : ICustomerSevice
    {
        MySQLEntities dc;
        public CustomerSevice()
        {
            dc = new MySQLEntities();
        }

        public List<customer> GetAllCustomer()
        {
            var query = (from a in dc.customers
                         select a).Distinct().Include(c=>c.customerpreference);

            List<customer> CustomersList = new List<customer>();

            query.ToList().ForEach(x =>
            {
                CustomersList.Add(new customer
                {
                    Id_Cus = x.Id_Cus,
                    FirstName_Cus = x.FirstName_Cus,
                    LastName_Cus = x.LastName_Cus,
                    PhoneNum_Cus = x.PhoneNum_Cus,
                    Email_Cus = x.Email_Cus,
                });
            });
            return CustomersList;
        }

        public customer CustomerDetails(string Id_Cus)
        {
            customer Cust = new customer();
            try
            {
                var query = (from a in dc.customers
                             where a.Id_Cus.Equals(Id_Cus)
                             select a).Distinct().FirstOrDefault();
                Cust.Id_Cus = query.Id_Cus;
                Cust.FirstName_Cus = query.FirstName_Cus;
                Cust.LastName_Cus = query.LastName_Cus;
                Cust.PhoneNum_Cus = query.PhoneNum_Cus;
                Cust.Email_Cus = query.Email_Cus;
            }
            catch (Exception ex)
            {
                throw new FaultException<string>(ex.Message);
            }
            return Cust;
        }

        // DELETE

        public void DeleteCustomer(string Id_Cus)
        {
            //MySQLEntities Cust = new MySQLEntities(); //check the file Model.edmx->ModelContext.tt->MySQLEntitys

            int k = Convert.ToInt32(Id_Cus);
            customer cur = (from n in dc.customers
                            where n.Id_Cus == k
                            select n).ToList().First();

            dc.Configuration.ValidateOnSaveEnabled = false;
            dc.customers.Remove(cur);
            dc.SaveChanges();
        }

        //Insert/POST

        public void InsertCustomer(customer customerDataContract)
        {
            //MySQLEntities Cust = new MySQLEntities();
            customer cust = new customer();

            cust.Id_Cus = Convert.ToInt32(customerDataContract.Id_Cus);
            cust.FirstName_Cus = customerDataContract.FirstName_Cus;
            cust.LastName_Cus = customerDataContract.LastName_Cus;
            cust.PhoneNum_Cus = Convert.ToInt32(customerDataContract.PhoneNum_Cus);
            cust.Email_Cus = customerDataContract.Email_Cus;
            dc.customers.Add(cust);
            dc.SaveChanges();
        }

        //Update/PUT
        public void UpdateCustomer(customer customerDataContract)
        {
            //using (CustomerDataContract Cust = new CustomerDataContract())
            //using (MySQLEntities Cust = new MySQLEntities()) 
            {
                int k = Convert.ToInt32(customerDataContract.Id_Cus);
                customer cust = dc.customers.Where(n => n.Id_Cus == k).Include(a=>a.customerpreference).FirstOrDefault();

                cust.Id_Cus = Convert.ToInt32(customerDataContract.Id_Cus);
                cust.FirstName_Cus = customerDataContract.FirstName_Cus;
                cust.LastName_Cus = customerDataContract.LastName_Cus;
                cust.PhoneNum_Cus = Convert.ToInt32(customerDataContract.PhoneNum_Cus);
                cust.Email_Cus = customerDataContract.Email_Cus;

                dc.SaveChanges();
            }
        }
    }
}
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace WcfRestFullService.Model
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.Web;

    [DataContract]
    public partial class customerpreference
    {
        [DataMember]
        public int Id_Cus { get; set; }
        [DataMember]
        public int Id_Res { get; set; }
        [DataMember]
        public string Name_Dis { get; set; }
        [DataMember]
        public int Id_Type { get; set; }

        public virtual customer customer { get; set; }
        public virtual order order { get; set; }
        public virtual type_dishes type_dishes { get; set; }
    }
}

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated from a template.
//
//     Manual changes to this file may cause unexpected behavior in your application.
//     Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace WcfRestFullService.Model
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.Web;

    [DataContract]
    public partial class customer
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public customer()
        {
            this.dishesrankings = new HashSet<dishesranking>();
            this.orders = new HashSet<order>();
        }

        [DataMember]
        public int Id_Cus { get; set; }
        [DataMember]
        public string FirstName_Cus { get; set; }
        [DataMember]
        public string LastName_Cus { get; set; }
        [DataMember]
        public int PhoneNum_Cus { get; set; }
        [DataMember]
        public string Email_Cus { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<dishesranking> dishesrankings { get; set; }
        public virtual customerpreference customerpreference { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<order> orders { get; set; }
    }
}

введите описание изображения здесь

введите описание изображения здесь

введите описание изображения здесь

1 Ответ

0 голосов
/ 07 апреля 2020

Свойство вашего внешнего ключа в таблице должно быть

Public ICollection<CustomerDataContract> IdCus {get;set;}
...