我正在使用EF Code First来查询和创建数据库。我的一个实体(关系)具有两个到同一实体(活动)的导航属性。我的问题是,如果我使用EF来创建数据库模式,它将创建四个外键列和约束而不是两个。
以下是相关的代码部分:
活动类:
Chrome 47.0.2526 (Windows 10 0.0.0) DebugDirective should compile in component template FAILED
Failed: The selector "#root0" did not match any elements
Error: The selector "#root0" did not match any elements
at new BaseException (C:/~/node_modules/angular2/bundles/angular2.dev.js:8080:21)
at DomRenderer_.createRootHostView (C:/~/node_modules/angular2/bundles/angular2.dev.js:15248:15)
at AppViewManager_.createRootHostView (C:/~/node_modules/angular2/bundles/angular2.dev.js:11265:52)
at C:/~/node_modules/angular2/bundles/angular2.dev.js:14531:46
at Zone.run (C:/~/node_modules/angular2/bundles/angular2-polyfills.js:138:17)
at Zone.run (C:/~/node_modules/angular2/bundles/testing.dev.js:2544:30)
at zoneBoundFn (C:/~/node_modules/angular2/bundles/angular2-polyfills.js:111:19)
at lib$es6$promise$$internal$$tryCatch (C:/~/node_modules/angular2/bundles/angular2-polyfills.js:1511:16)
at lib$es6$promise$$internal$$invokeCallback (C:/~/node_modules/angular2/bundles/angular2-polyfills.js:1523:17)
at lib$es6$promise$$internal$$publish (C:/~/node_modules/angular2/bundles/angular2-polyfills.js:1494:11)
at C:/~/node_modules/angular2/bundles/angular2-polyfills.js:243:5
at microtask (C:/~/node_modules/angular2/bundles/testing.dev.js:2557:15)
at Zone.run (C:/~/node_modules/angular2/bundles/angular2-polyfills.js:138:17)
at Zone.run (C:/~/node_modules/angular2/bundles/testing.dev.js:2544:30)
at zoneBoundFn (C:/~/node_modules/angular2/bundles/angular2-polyfills.js:111:19)
at lib$es6$promise$asap$$flush (C:/~/node_modules/angular2/bundles/angular2-polyfills.js:1305:9)
Failed: Cannot read property 'hostView' of undefined
TypeError: Cannot read property 'hostView' of undefined
at new ComponentFixture_ (C:/~/node_modules/angular2/bundles/testing.dev.js:2047:97)
at C:/~/node_modules/angular2/bundles/testing.dev.js:2145:16
at Zone.run (C:/~/node_modules/angular2/bundles/angular2-polyfills.js:138:17)
at Zone.run (C:/~/node_modules/angular2/bundles/testing.dev.js:2544:30)
at zoneBoundFn (C:/~/node_modules/angular2/bundles/angular2-polyfills.js:111:19)
at lib$es6$promise$$internal$$tryCatch (C:/~/node_modules/angular2/bundles/angular2-polyfills.js:1511:16)
at lib$es6$promise$$internal$$invokeCallback (C:/~/node_modules/angular2/bundles/angular2-polyfills.js:1523:17)
at lib$es6$promise$$internal$$publish (C:/~/node_modules/angular2/bundles/angular2-polyfills.js:1494:11)
at C:/~/node_modules/angular2/bundles/angular2-polyfills.js:243:5
at microtask (C:/~/node_modules/angular2/bundles/testing.dev.js:2557:15)
at Zone.run (C:/~/node_modules/angular2/bundles/angular2-polyfills.js:138:17)
at Zone.run (C:/~/node_modules/angular2/bundles/testing.dev.js:2544:30)
at zoneBoundFn (C:/~/node_modules/angular2/bundles/angular2-polyfills.js:111:19)
at lib$es6$promise$asap$$flush (C:/~/node_modules/angular2/bundles/angular2-polyfills.js:1305:9)
Failed: Cannot read property 'detectChanges' of undefined
TypeError: Cannot read property 'detectChanges' of undefined
at C:/~/dist/scripts/core/components/people/people.component.spec.js:17:32
at C:/~/dist/scripts/core/components/people/people.component.spec.js:17:32
at Zone.run (C:/~/node_modules/angular2/bundles/angular2-polyfills.js:138:17)
at Zone.run (C:/~/node_modules/angular2/bundles/testing.dev.js:2544:30)
at zoneBoundFn (C:/~/node_modules/angular2/bundles/angular2-polyfills.js:111:19)
at lib$es6$promise$$internal$$tryCatch (C:/~/node_modules/angular2/bundles/angular2-polyfills.js:1511:16)
at lib$es6$promise$$internal$$invokeCallback (C:/~/node_modules/angular2/bundles/angular2-polyfills.js:1523:17)
at lib$es6$promise$$internal$$publish (C:/~/node_modules/angular2/bundles/angular2-polyfills.js:1494:11)
at C:/~/node_modules/angular2/bundles/angular2-polyfills.js:243:5
at microtask (C:/~/node_modules/angular2/bundles/testing.dev.js:2557:15)
at Zone.run (C:/~/node_modules/angular2/bundles/angular2-polyfills.js:138:17)
at Zone.run (C:/~/node_modules/angular2/bundles/testing.dev.js:2544:30)
at zoneBoundFn (C:/~/node_modules/angular2/bundles/angular2-polyfills.js:111:19)
at lib$es6$promise$asap$$flush (C:/~/node_modules/angular2/bundles/angular2-polyfills.js:1305:9)
Chrome 47.0.2526 (Windows 10 0.0.0): Executed 16 of 16 (1 FAILED) (0.396 secs / 0.167 secs)
关系类:
public class Activity {
public virtual ICollection<Relationship> Successors { get; set; }
public virtual ICollection<Relationship> Predecessors { get; set; }
}
关系映射类:
public class Relationship {
public virtual Activity Activity1 { get; set; }
public int Activity1_ID { get; set; }
public virtual Activity Activity2 { get; set; }
public int Activity2_ID { get; set; }
}
有没有办法阻止创建最后两列?
答案 0 :(得分:3)
这应该只创建2个外键列。
public class Activity
{
public int Id { set; get; }
public virtual ICollection<Relationship> Successors { get; set; }
public virtual ICollection<Relationship> Predecessors { get; set; }
}
public class Relationship
{
public int Id { set; get; }
public virtual Activity Activity1 { get; set; }
public int Activity1_ID { get; set; }
public virtual Activity Activity2 { get; set; }
public int Activity2_ID { get; set; }
}
我在OnModelCreating
指定关系/ FK性质的DbContext类。
public class MyDb: DbContext
{
public MyDb():base("EfDbContext")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Relationship>()
.HasRequired(f => f.Activity1)
.WithMany(f => f.Predecessors)
.HasForeignKey(g => g.Activity1_ID)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Relationship>().
HasRequired(f => f.Activity2)
.WithMany(f => f.Successors)
.HasForeignKey(g => g.Activity2_ID)
.WillCascadeOnDelete(false);
}
}