编程技术网

关注微信公众号,定时推送前沿、专业、深度的编程技术资料。

 找回密码
 立即注册

QQ登录

只需一步,快速开始

极客时间

实体框架中的关系(数据库优先):Relationship (database first) in Entity Framework

Tadimsky 数据库/缓存 2022-5-11 10:23 5人围观

腾讯云服务器
实体框架中的关系(数据库优先)的处理方法

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

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

我的第一张桌子

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

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),但没有导入第二张表中

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

可能是

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

问题解答

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

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.

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

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是一种用于运行脚本以对数据库进行更改的工具,该工具允许进行版本控制和回滚,如果您首先使用数据库,则它非常有用.

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.

这篇关于实体框架中的关系(数据库优先)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程技术网(www.editcode.net)!

腾讯云服务器 阿里云服务器
关注微信
^