EntityFramework之一對一關聯性(二)

來源:互聯網
上載者:User

標籤:

下面建立兩個實體,關係為一對一

Product為產品類,WarrantyCard為保修卡類,一個產品對應一個保修卡,一個保修卡對應一個產品。

1、建立實體類

 1     public class Product 2     { 3         public int Id { get; set; } 4         public string Name { get; set; } 5         public string Description { get; set; } 6  7         public virtual WarrantyCard WarrantyCard { get; set; } 8     } 9 10     public class WarrantyCard11     {12         public int ProductId { get; set; }13         public DateTime ExpiredDate { get; set; }14 15         public virtual Product Product { get; set; }16     }

2、建立EntityContext類並繼承DbContext

 1     public class EntityContext : DbContext 2     { 3         public EntityContext() 4             : base("name=DBConnectionString") 5         { 6  7         } 8  9         public DbSet<Product> Product { get; set; }10         public DbSet<WarrantyCard> WarrantyCard { get; set; }11 12         protected override void OnModelCreating(DbModelBuilder modelBuilder)13         {14             modelBuilder.Configurations.AddFromAssembly(Assembly.GetExecutingAssembly());15             base.OnModelCreating(modelBuilder);16         }

 3、在web.config檔案中添加資料庫連接字串

1 <connectionStrings>2     <add name="DBConnectionString" connectionString="Data Source=.;Initial Catalog=OneToOne;Integrated Security=true" providerName="System.Data.SqlClient" />3 </connectionStrings>

 4、建立表映射類

 1     public class ProductMap : EntityTypeConfiguration<Product> 2     { 3         public ProductMap() 4         { 5             ToTable("Product"); 6             HasKey(p => p.Id); 7  8             //兩條語句效果一樣 9             HasOptional(p => p.WarrantyCard).WithRequired(p => p.Product);10             //HasRequired(p => p.WarrantyCard).WithRequiredPrincipal(i => i.Product);11         }12     }13 14     public class WarrantyCardMap : EntityTypeConfiguration<WarrantyCard>15     {16         public WarrantyCardMap()17         {18             ToTable("WarrantyCard");19             HasKey(w => w.ProductId);20         }21     }

 HasOptional(p => p.WarrantyCard).WithRequired(p => p.Product)這句大概意思是:在新增資料到Product表時可以不加資料到WarrantyCard表,也可以加,

但是往WarrantyCard表新增資料時必須有Product表中的ProductId,WarrantyCard依賴Product,產生的是關係是Product表Id是主鍵,WarrantyCard表ProductId是外鍵。

5、寫點代碼測試一下

 1     EntityContext db = new EntityContext(); 2     var product = new Product() { Description = "電器", Name = "電磁爐" }; 3     db.Set<Product>().Add(product); 4     db.SaveChanges(); 5  6     var card = new WarrantyCard() { ExpiredDate = DateTime.Now, ProductId = product.Id }; 7     db.Set<WarrantyCard>().Add(card); 8     db.SaveChanges(); 9 10     //WarrantyCard為Product類中的WarrantyCard屬性名稱11     var list = db.Set<Product>().Include("WarrantyCard").ToList();

 6、資料庫圖表

 

EntityFramework之一對一關聯性(二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.