简体   繁体   English

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

[英]Relationship (database first) in Entity Framework

I created a database, then applied dataase-first.我创建了一个数据库,然后首先应用了 dataase。 Then it automatically imported the database to VS.然后它自动将数据库导入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?)我读到了流利的 api 以及您可以直接在表类中指定键和属性的事实(什么时候通过流利更好,什么时候直接指定?)

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 MySQL实体

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在这里我创建了数据(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();


        }

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

Yes it does automatically model foreign keys.是的,它会自动执行 model 外键。 You can see that it has done so in your model because there are navigation properties such as dishesrankings in your Customer class.您可以在您的 model 中看到它已经这样做了,因为您的客户 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.我们将通过使用 dbup 更改数据库来更新它,然后从数据库中更新 model。 This way you ensure consistency between the model and the database.这样可以确保 model 和数据库之间的一致性。

DbUp: https://dbup.github.io/数据库: 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. DbUp 是一个运行脚本以更改数据库的工具,允许版本控制和回滚,如果您首先使用数据库,它非常有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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