DataAnnotations與Fluent API方法做同樣的事情嗎?流利的API和數據註釋
例如,.HasRequired == [必需]?
.HasKey == [Key]?
如果我不希望我的表pk字段生成爲ClassNameClassNameID,如果我爲我的pk屬性使用ClassNameID格式,我必須用[Key]標記類鍵還是使用.HasKey?
我可以使用DataAnnotations和Fluent API的組合;或者,它是一個還是另一個?
我必須映射M:這兩個類m關係參與即
public class Foo
{
public ICollection<Bar> Bars
}
public class Bar
{
public ICollection<Bar> Foos
}
public class Context : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Foo>()
.HasMany(f => f.Bars).WithMany(b => b.Foos)
.Map(t =>
{
t.MapLeftKey("FooID");
t.MapRightKey("BarId");
t.ToTable("FooBar");
});
modelBuilder.Entity<Bar>()
.HasMany(b => b.Foos).WithMany(f => f.Bars)
.Map(t =>
{
t.MapLeftKey("FooID");
t.MapRightKey("BarId");
t.ToTable("FooBar");
});
}
感謝。
短而甜。謝謝。 :) – Erik 2012-07-13 19:41:47