简体   繁体   中英

Entity Framework/ table relationships

I have a table in which all the fields are registered, for example, in my table "Id_Cus" is the primary key. How to correctly indicate that in the 2 table "Id_Cus" was taken from 1 table? I attach screenshots

I attached classes where field properties are registered. However, in the first table, the implementation of these fields is embedded in the interface. Tell me where exactly should I indicate that the "Id_Cus" in the second table should be taken from the first table (in the class itself, as in the screenshot or in the interface) and how to do it?

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; }
    }
}

enter image description here

enter image description here

enter image description here

The Property of your foreign key in table should be

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM