Entity Framework Code First - virtual column identifier

I am using EF Code First (4.3.1) in a personal ASP.NET MVC 3 project with a very simple domain model, and I'm almost at the point where EF will generate the DB schema as I want it.

The domain model has two classes: Painting and Gallery . Each Painting belongs to one Gallery , and Gallery has two virtual properties that point to Painting : one to indicate which of the paintings is the cover, and one of them - the image of the slider is displayed on the main page.

The classes are as follows. I removed some annotations and irrelevant properties to make them readable.

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

and painting:

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

It generates the correct db schema for both classes and its relationships, the only small problem I have is that I have not found a way to manage the column names that it gives the Cover and Slider virtual properties in the Gallery table.

He will call them Cover_PaintingId and Slider_PaintingId .

I tried using the [Column("columnNameHere")] attribute, but that doesn't affect it at all. As in "I typed a certain unrelated word, and it did not appear in the pattern."

I would call it CoverPaintingId without underlining.

Any help is appreciated. Thanks

+6
source share
2 answers

The ForeignKey attribute allows you to define a property that will act as your foreign key if you want it to exist in your model:

 [ForeignKey("CoverPaintingId")] public virtual Painting Cover { get; set; } public int? CoverPaintingId { get; set; } 

Note. You can put the attribute either in a virtual property on a foreign key - you just need to specify the name of the "other".

However, since you will have two connections between the same set of objects, you cannot do this without disabling cascading deletes on one or both of them. This can only be done using the 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); } } 

If you do not want the foreign key properties to exist in your code model, you can also configure them with . Map (...) before WillCascadeOnDelete (false) part of the API instead of using ForeignKey. I prefer using a Foreign Key, but here is what the code looks like if you want to do it like this:

 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); } } 
+6
source

you can try to use [inverseproperty] decoration to achieve this.

0
source

Source: https://habr.com/ru/post/922306/


All Articles