为什么我的实体框架应用程序可以运行?
为了快速了解它,模型是:
public class foo{
public int ID;
[Required]
public bla bla;
}
public class bla{
public int ID;
public string test
}
基本上,这工作正常并且符合预期。
我的应用程序是一个普通应用程序 - 我有一个 bla
列表,每个列表都与 foo
关联。
我有一个包含 Foo 列表的页面,并且有人可以单击其中一个,然后将显示与该 Foo 关联的每个 bla。
该代码将 Foo 的 ID 传递给它,并且是:
bla bla = db.bla.Where(x=>x.id == id);
db.foo.Where(x=>x.bla == bla);
但是,我真的想尝试 FluentAPI,并且我使用了以下内容:
modelBuilder.Entity<foo>()
.HasRequired(x => x.bla)
.WithOptional()
.WillCascadeOnDelete();
通过查看数据库模式,我猜测我无意中创建了一对一关系,因为 bla_id
列不存在。然而,我真正不明白的是我的应用程序如何在不修改的情况下继续工作? (不管怎样,我最多只能为每个 bla 创建一个 foo)。
我真的不明白为什么 WithOptional
会暗示一对一 - 这真的让我很恼火,因为不同选项(和 MSDN)的工具提示暗示非常相似,即使不是相同的信息选项。这使得学习变得非常困难。
另外,我认为 FluentAPI 完全覆盖模型注释是否正确?
最后,我在这里有点挣扎。有谁知道 FluentAPI 的备忘单/列表吗?
To quickly get in to it, the models are:
public class foo{
public int ID;
[Required]
public bla bla;
}
public class bla{
public int ID;
public string test
}
Basically, this works fine and as expected.
My application is a normal application - I have a list of bla
's each of which is associated with a foo
.
I have a page that has a list of Foos, and, someone can click on one which will then show every bla that is associated with that Foo.
The code has the ID of the Foo passed to it and is:
bla bla = db.bla.Where(x=>x.id == id);
db.foo.Where(x=>x.bla == bla);
However, I really wanted to experiment with the FluentAPI, and, I used the following:
modelBuilder.Entity<foo>()
.HasRequired(x => x.bla)
.WithOptional()
.WillCascadeOnDelete();
I am guessing from looking at the database schema that I have inadvertently created a one to one relationship because the bla_id
column doesn't exist. However, what I really don't understand is how my application continues to work without modification? (all be it, I can only create a maximum of one foo per bla).
I don't really understand why WithOptional
would imply one to one - this is really getting on my nerves as the tooltips for the different options (and MSDN) imply very similar, if not the same information between the options. It is making it very hard to learn.
Also, am I right in thinking that the FluentAPI overwrites model annotations completely?
Lastly, I have been struggling here a bit. Does anyone know of a cheat sheet/list for the FluentAPI?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看 使用 Fluent API 配置关系(代码首先)。
向下滚动到配置一对零或一关系。
Take a look at Configuring Relationships with Fluent API (Code First).
Scroll down to Configuring a One-to–Zero-or-One Relationship.