实体框架中的关系(数据库优先)

时间:2020-04-13 12:33:14

标签: c# entity-framework entity-framework-6 entity-framework-core relationship

我创建了一个数据库,然后首先应用了dataase。然后,它自动将数据库导入到VS。请告诉我,何时数据库优先自动指示关系?可能不是,我的数据没有被导入。您能告诉我如何正确建立连接吗?我读了有关fluent api的知识,以及您可以在表类中直接指定键和属性的事实(什么时候最好通过fluent进行操作,何时直接指定?)

我的第一张桌子

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

我的第二张桌子

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

我在这里创建数据(Id_Cus),但是它没有导入第二张表中

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


        }

可能是

中的问题
 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 :(得分:0)

是的,它会自动为外键建模。您可以看到它已经在模型中完成了,因为在Customer类中有导航属性,例如餐具排名。

我们有一个数据库优先项目。我们将通过使用dbup更改数据库,然后从数据库更新模型来更新它。这样,您可以确保模型和数据库之间的一致性。

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

DbUp是一种用于运行脚本以对数据库进行更改的工具,该工具允许进行版本控制和回滚,如果您首先使用数据库,则它非常有用。