Entity framework 4 如何在EF CTP5中映射属性
在CTP 4中,我们可以选择要映射的属性,如下所示:Entity framework 4 如何在EF CTP5中映射属性,entity-framework-4,entity-framework-ctp5,Entity Framework 4,Entity Framework Ctp5,在CTP 4中,我们可以选择要映射的属性,如下所示: this.MapSingleType(i => new { i.Id, i.OriginalFileName, i.Extension, i.MimeType, i.Width, i.Height, i.ImageStoreLocationId, i.AlternateText, i.ImageData }); 我们如何在CTP5中实现这一点 我尝试使用以
this.MapSingleType(i => new
{
i.Id,
i.OriginalFileName,
i.Extension,
i.MimeType,
i.Width,
i.Height,
i.ImageStoreLocationId,
i.AlternateText,
i.ImageData
});
我们如何在CTP5中实现这一点
我尝试使用以下映射配置,但这似乎不起作用,因为我仍然必须显式忽略(this.ignore(..)我不希望映射的属性:
Map(config =>
{
config.Properties(i => new
{
i.OriginalFileName,
i.Extension,
i.MimeType,
i.Width,
i.Height,
i.ImageStoreLocationId,
i.AlternateText,
i.ImageData
});
config.ToTable("Images");
});
考虑到新的API应该更加流畅,奇怪的是我必须编写更多的代码来实现同样的功能
谢谢
BenCTP5在数据注释和流畅的API方面确实更强大、更灵活。例如,在CTP4中,如果我们想从映射中排除一个属性,我们必须使用MapSingleType显式映射其他所有内容,以便跳过我们不想要的属性,就像您提到的那样。
在CTP5中,这可以通过在属性上使用
[NotMapped]
属性或通过以下流畅的API代码来实现:
this.Ignore(i => i.Id);
完成后,无需调用
Map
方法。这篇博客文章有CTP5映射示例
使clr可为Null属性成为必需的:
modelBuilder.Entity<Product>()
.Property(p => p.Name)
.IsRequired();
modelBuilder.Entity()
.Property(p=>p.Name)
.IsRequired();
更改字符串长度:
modelBuilder.Entity<Product>()
.Property(p => p.Name)
.HasMaxLength(50);
modelBuilder.Entity()
.Property(p=>p.Name)
.HasMaxLength(50);
关闭标识:
modelBuilder.Entity<Product>()
.Property(p => p.ProductId)
.HasDatabaseGenerationOption(DatabaseGenerationOption.None);
modelBuilder.Entity()
.Property(p=>p.ProductId)
.HasDatabaseGenerationOption(DatabaseGenerationOption.None);
忽略属性:
modelBuilder.Entity<Person>()
.Ignore(p => p.Name);
modelBuilder.Entity()
.Ignore(p=>p.Name);
表和列映射
更改列名:
modelBuilder.Entity<Category>()
.Property(c => c.Name)
.HasColumnName("cat_name");
modelBuilder.Entity()
.Property(c=>c.Name)
.HasColumnName(“猫名”);
更改表名:
modelBuilder.Entity<Category>()
.ToTable("MyCategories");
modelBuilder.Entity()
.ToTable(“我的类别”);
使用架构更改表名:
modelBuilder.Entity<Category>()
.ToTable("MyCategories", "sales");
modelBuilder.Entity()
.ToTable(“我的类别”、“销售”);
是的,我意识到我可以忽略这样的属性。我可以不再像以前那样映射一组属性吗?在某些情况下,这比依赖惯例更可取。啊,现在我明白你的意思了。是的,你是对的,似乎你必须明确地忽略这个财产,否则它会抱怨。有了[NotMapped]
属性,这并不坏。是的,我看过这篇文章,但它没有解释如何映射一组属性。在这种情况下,我不想依赖约定来映射我的类。不确定一组属性是什么意思?如果需要映射属性,那么新的api是modelBuilder.Entity().Property(c=>c.Name).HasColumnName(“cat_Name”);