实体框架代码优先 - 虚拟财产列命名

GR7*_*GR7 6 .net c# entity-framework-4

我在个人ASP.NET MVC 3项目上使用EF Code First(4.3.1),使用一个非常简单的域模型,我几乎就在EF将按照我希望的方式生成数据库模式.

域模型有两个类:PaintingGallery.每个都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; }
}
Run Code Online (Sandbox Code Playgroud)

和绘画:

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; }
}
Run Code Online (Sandbox Code Playgroud)

它生成两个类及其关系的正确DB模式,唯一的小问题,我是,我还没有找到一种方法来控制它给的虚拟财产的列名Cover,并SliderGallery表中.

这将他们的名字Cover_PaintingIdSlider_PaintingId.

我尝试使用该[Column("columnNameHere")]属性,但这根本不会影响它.正如在"我键入了一个非相关的单词并且它没有出现在模式中".

我想用它来命名CoverPaintingId,没有下划线.

任何帮助是极大的赞赏.谢谢

Ric*_*ard 6

ForeignKey的属性,您可以定义,如果你想让它在模型中存在,这将作为你的外键行为属性:

[ForeignKey("CoverPaintingId")]
public virtual Painting Cover { get; set; }
public int? CoverPaintingId { get; set; }
Run Code Online (Sandbox Code Playgroud)

请注意,您可以将属性放在外键的虚拟属性上 - 只需指定"其他"的名称即可.

但是,由于您在同一组实体之间将有两个关系,因此如果不在其中一个或两个上禁用级联删除,您将无法执行此操作.这只能使用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);
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您不希望代码模型中存在外键属性,则还可以在之前使用.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);
    }
}
Run Code Online (Sandbox Code Playgroud)