Relationship (database first) in Entity Framework

c# entity-framework entity-framework-6 entity-framework-core relationship

Question

I created a database, then applied dataase-first. Then it automatically imported the database to VS. Please tell me, when database-first automatically indicates relationship? Probably not, my data is not being imported. Could you tell me how to establish connections correctly? I read about the fluent api and about the fact that you can specify keys and properties directly in the table classes (And when is it better to do through fluent, and when to specify directly?)

My 1st table

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

My 2nd table

namespace WcfRestFullService.Model
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    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; }
    }
}

MySQLEntities

namespace WcfRestFullService.Model
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;

    public partial class MySQLEntities : DbContext
    {
        public MySQLEntities()
            : base("name=MySQLEntities")
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<customer>()
        .HasMany(c => c.customerpreference)
        .WithOptional(o => o.Customer);
            //throw new UnintentionalCodeFirstException();//here problem
        }

        public virtual DbSet<customer> customers { get; set; }
        public virtual DbSet<customerpreference> customerpreferences { get; set; }
        public virtual DbSet<dish> dishes { get; set; }
        public virtual DbSet<dishesranking> dishesrankings { get; set; }
        public virtual DbSet<ingridient> ingridients { get; set; }
        public virtual DbSet<order> orders { get; set; }
        public virtual DbSet<restaraunt> restaraunts { get; set; }
        public virtual DbSet<type_dishes> type_dishes { get; set; }
        public object Parameters { get; internal set; }
    }
}

Here I create data(Id_Cus) but it doesn't import in 2nd table

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

            customerpreference custPref = new customerpreference()
            {
                Id_Cus = customerDataContract.Id_Cus,
                Id_Res = 0, // some value
                Name_Dis = null, // some value
                Id_Type = 0 // some value
            };
            dc.customerpreferences.Add(custPref);

            dc.SaveChanges();

            int k = Convert.ToInt32(cust.Id_Cus);
            customer custFromDb =(from n in dc.customers
                                  where n.Id_Cus == k
                                  select n).Include(c => c.customerpreference).First();


        }

perhaps problem in

 cust = (from n in dc.customers
                    where n.Id_Cus == k
                    select n).Include(c =>c.customerpreference).ToList().First();
            dc.customers.Add(cust);
            dc.SaveChanges();
1
0
4/14/2020 7:32:51 AM

Popular Answer

Yes it does automatically model foreign keys. You can see that it has done so in your model because there are navigation properties such as dishesrankings in your Customer class.

We had a database first project. We would update it by changing the database using dbup and then updating the model from the database. This way you ensure consistency between the model and the database.

DbUp: https://dbup.github.io/

DbUp is a tool to run scripts to make changes to the database that allows versioning and rollback and it is very useful if you're using database first.

0
4/13/2020 1:41:20 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow