EF代码第一个自动外键关联问题
通过本教程,我首先基本掌握了最新版本的 EF 代码 - http://www.asp.net/mvc/tutorials/getting-started-with-mvc3-part4-cs 但我对一方面有点困惑,想知道是否有人可以摆脱点亮它吗?解释一下 - 有一个名为“Site”的类,我想要一个名为“HomePageId”的字段,然后该字段应映射到具有该 ID 的“SitePage”对象。看起来很简单吗?但是当 EF 创建 Db 和关系时,它似乎并不理解这一点。我确信我做错了 - 这是代码:
public class Site
{
public int SiteId { get; set; }
public string SiteName { get; set; }
public string SiteUrlPortion { get; set; }
// Relationship - SitePages
public virtual ICollection<SitePage> SitePages { get; set; }
// Relationship - HomePage
public int HomePageId { get; set; }
public virtual SitePage HomePage { get; set; }
}
public class SitePage
{
public int SitePageId { get; set; }
public string SitePageTitle { get; set; }
public string SitePageUrlPortion { get; set; }
// Relationship - Site
public int SiteId { get; set; }
public virtual Site Site { get; set; }
}
“SitePage”类按照您的预期生成返回到“Site”的关系。但是我在两个表中的列方面得到的不仅没有意义,而且代码方面的关系也没有按预期工作。 (例如,当我给“站点”一个“HomePageId”时,站点的“HomePage”为空。
显然,文档方面几乎没有,因为这仍在开发中,但只是想知道是否有人有任何想法?我需要开始用属性装饰属性?还是我要求它理解一些它永远不会理解的东西?!
无论如何,我会坚持下去并回发我明显发现的任何内容。 抢
I've got a basic grip of the latest version of EF code first via this tutorial - http://www.asp.net/mvc/tutorials/getting-started-with-mvc3-part4-cs but I'm slightly confused about one aspect and wondering if anybody could shed light on it? To explain - there's a class called "Site" which I want to have a field called "HomePageId" which should then map to the "SitePage" object with that Id. Seems simple enough? But when EF creates the Db and the relationships it doesn't seem to understand this. I'm sure it's something I'm doing wrong - here's the code:
public class Site
{
public int SiteId { get; set; }
public string SiteName { get; set; }
public string SiteUrlPortion { get; set; }
// Relationship - SitePages
public virtual ICollection<SitePage> SitePages { get; set; }
// Relationship - HomePage
public int HomePageId { get; set; }
public virtual SitePage HomePage { get; set; }
}
public class SitePage
{
public int SitePageId { get; set; }
public string SitePageTitle { get; set; }
public string SitePageUrlPortion { get; set; }
// Relationship - Site
public int SiteId { get; set; }
public virtual Site Site { get; set; }
}
The "SitePage" class generates the relationship back to "Site" as you would expect. But what I've got in terms of columns in both tables not only doesn't make sense but the relationship from the code-side of things doesn't work as expected. (Eg when I give the "Site" a "HomePageId" the site's "HomePage" is null.
Obviously there's little out there in terms of documentation because this is still in development, but just wondering if anybody had any ideas? Do I need to start decorating the properties with Attributes? Or am I asking it to understand something that it never will?!
Thanks to all in advance. I'll persevere anyway and post back anything I find obviously.
Rob
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用像这样的ForeignKey属性标记您的HomePage属性,
您也可以使用流畅的配置,但不记得立即
try marking your HomePage property with a ForeignKey attribute like this
you could also use the fluent configuration but don't remember that offhand
这可能是 EF 的限制。该 EF 只能处理 2 个表之间的一种关系。
表之间有 2 个关系:与站点页面列表和与主页的关系。
尝试删除此行:
您仍然拥有 homepageid,因此在某种程度上此信息是多余的。
It is probably a limitation in EF. That EF can only handle one relationship between 2 tables.
You have 2 relationships between the tables, to the list of site pages and to the home page.
try removing this line:
You still have the homepageid, so in a way this information was redundant.