EF4:父表中的子引用。如何?
如果我有 2 个表:(
Parent: Id (PK), ChildId (FK)
Child : Id (PK)
表名称不具有代表性)
如何让 EF 创建表以便我可以将 ChildId 添加到父表中?
我所做的一切,都是创建这个:
Parent: Id (PK)
Child : Id (PK), ParentId (FK)
我不希望这样,因为子表将独立更新,并且下拉列表中使用的值供父表选择。
我可以做困难的事情,但简单的事情却让我难住了!
R。
If I have 2 tables:
Parent: Id (PK), ChildId (FK)
Child : Id (PK)
(Table names are not representative)
How do I get EF to create the tables so I can add a ChildId to the Parent table?
Everything I do, I get this created instead:
Parent: Id (PK)
Child : Id (PK), ParentId (FK)
I don't want that as the Child table will be updated independently and the values used in a dropdown list for the Parent to choose from.
I can do the hard stuff but the easy bits have me stumped!
R.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你说:
那么你说:
这有点令人困惑。您是否从数据库生成模型,执行模型优先、代码优先?
基于“2个表”,EF应该创建两个实体,“Parent”和“Child”。
基数将为:子级 1 - 1..* 父级。
Parent 上将有一个名为 Child 的导航属性,Child 上将有一个名为 Parents(注意复数)的导航属性>。
这确实没有意义。
我认为父母应该有许多孩子。
我认为你的 FK 方向不对。 ParentID 应该是 Child 的 FK。
进行这些更改,更新您的模型,如果仍有问题,请粘贴模型的屏幕截图,我们将从那里开始。
You say:
Then you say:
That's a bit confusing. Are you generating the model from the database, doing model-first, code-first?
Based on "the 2 tables", EF should create two entities, "Parent" and "Child".
The cardinality will be: Child 1 - 1..* Parent.
There will be a navigational property on Parent called Child, and a navigational property called Parents (note pluralization) on Child.
Which doesn't really make sense.
I would think a Parent should have many Children.
I think you have your FK's the wrong way around. ParentID should be a FK on Child.
Make those changes, update your model then if you still have issues, paste a screenshot of your model and we'll go from there.