[英]Entity Framework Code First - Virtual Property Column Naming
我在個人ASP.NET MVC 3項目上使用EF Code First(4.3.1),使用一個非常簡單的域模型,我幾乎就是EF將按照我希望的方式生成數據庫模式。
域模型有兩個類: Painting
和Gallery
。 每Painting
屬於一個Gallery
,而Gallery
有兩個虛擬屬性指向Painting
:一個用於指示哪個繪畫是封面圖像,另一個用於繪制哪個繪畫是主頁上顯示的滑塊圖像。
課程如下。 我刪除了一些注釋和不相關的屬性,使其可讀。
public class Gallery
{
public Gallery()
{
Paintings = new List<Painting>();
}
[ScaffoldColumn(false)]
[Key]
public int GalleryId { get; set; }
public string Name { get; set; }
[ScaffoldColumn(false)]
[Column("LaCover")]
public Painting Cover { get; set; }
[ScaffoldColumn(false)]
[Column("ElSlider")]
public Painting Slider { get; set; }
[ScaffoldColumn(false)]
public virtual List<Painting> Paintings { get; set; }
}
和繪畫:
public class Painting
{
[ScaffoldColumn(false)]
[Key]
public int PaintingId { get; set; }
public string Name { get; set; }
public int GalleryId { get; set; }
[Column("GalleryId")]
[ForeignKey("GalleryId")]
[InverseProperty("Paintings")]
public virtual Gallery Gallery { get; set; }
public string Filename { get; set; }
}
它為類及其關系生成了正確的數據庫模式,我唯一的小問題是我沒有找到一種方法來控制它為Gallery
表中的Cover
和Slider
的虛擬屬性提供的列名。
它將它們命名為Cover_PaintingId
和Slider_PaintingId
。
我嘗試使用[Column("columnNameHere")]
屬性,但這根本不會影響它。 正如在“我鍵入了一個非相關的單詞並且它沒有出現在模式中”。
我想把它命名為CoverPaintingId
,沒有下划線。
任何幫助是極大的贊賞。 謝謝
ForeignKey屬性允許您定義將充當外鍵的屬性(如果您希望它存在於模型中):
[ForeignKey("CoverPaintingId")]
public virtual Painting Cover { get; set; }
public int? CoverPaintingId { get; set; }
請注意,您可以將屬性放在外鍵的虛擬屬性上 - 只需指定“其他”的名稱即可。
但是 ,由於您在同一組實體之間將有兩個關系,因此如果不在其中一個或兩個上禁用級聯刪除,您將無法執行此操作。 這只能使用Fluent Configuration API完成 。
public class Gallery
{
public int GalleryId { get; set; }
[ForeignKey("CoverPaintingId")]
public virtual Painting Cover { get; set; }
public int? CoverPaintingId { get; set; }
[ForeignKey("SliderPaintingId")]
public virtual Painting Slider { get; set; }
public int? SliderPaintingId { get; set; }
}
public class Painting
{
public int PaintingId { get; set; }
}
public class MyContext : DbContext
{
public DbSet<Gallery> Galleries { get; set; }
public DbSet<Painting> Paintins { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Gallery>().HasOptional(g => g.Cover).WithMany().WillCascadeOnDelete(false);
modelBuilder.Entity<Gallery>().HasOptional(g => g.Slider).WithMany().WillCascadeOnDelete(false);
}
}
如果您不希望代碼模型中存在外鍵屬性,則還可以在之前使用.Map(...)來配置它們。 WillCascadeOnDelete(false)部分API而不是使用ForeignKey。 我更喜歡使用外鍵,但如果你想這樣做,代碼就是這樣的:
public class Gallery
{
public int GalleryId { get; set; }
public virtual Painting Cover { get; set; }
public virtual Painting Slider { get; set; }
}
public class Painting
{
public int PaintingId { get; set; }
}
public class MyContext : DbContext
{
public DbSet<Gallery> Galleries { get; set; }
public DbSet<Painting> Paintins { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Gallery>().HasOptional(g => g.Cover).WithMany().Map(m => m.MapKey("CoverPaintingId")).WillCascadeOnDelete(false);
modelBuilder.Entity<Gallery>().HasOptional(g => g.Slider).WithMany().Map(m => m.MapKey("SliderPaintingId")).WillCascadeOnDelete(false);
}
}
您可以嘗試使用[inversepropert]裝飾來實現這一目標。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.