实体架构
使用 VS2010、.NET4.0、MVC3、EF4.1 Code-First
我有这个 POCO 实体:
public class XBLContent
{
[Key]
[StringLength(36, ErrorMessage="Must have 36 characters")]
[Required(ErrorMessage="Must have a unique GUID")]
public string GUID { get; set; }
public int Price { get; set; }
public float FileSize { get; set; }
public virtual ICollection<XBLRegionalContent> RegionalInfo { get; set; }
public string RelatedGameId { get; set; }
[ForeignKey("RelatedGameId")]
public virtual XBLContent RelatedGame { get; set; }
}
public class XBLRegionalContent
{
[Key, Column(Order = 0)]
public string ContentId { get; set; }
[ForeignKey("ContentId")]
public virtual XBLContent Content { get; set; }
[Key, Column(Order = 1)]
public string RegionId { get; set; }
[ForeignKey("RegionId")]
public virtual XBLRegion Region { get; set; }
public string Name { get; set; }
}
public class XBLRegion
{
[Key]
[StringLength(5, ErrorMessage="ID must have 5 characters")]
[Required]
[RegularExpression(@"[a-z|A-Z]{2}-[A-Z|a-z]{2}")]
public string ID { get; set; }
public string Country { get; set; }
public string Language { get; set; }
}
关系:
- 一个 XBLContent 有许多 XBLRegionalContent;
- 一个 XBLContent 可以与另一个 XBLContent 相关(大多数不是);
- 1个XBLRegionalContent有1个XBLContent和1个XBLRegion;
- 一个XBLRegion有多个XBLRegionalContent;
Context 对象非常简单:
public class XBLContentContext : DbContext
{
public DbSet<XBLContent> XBLContents { get; set; }
public DbSet<XBLRegionalContent> XBLRegionalInfos { get; set; }
public DbSet<XBLRegion> XBLRegion { get; set; }
public XBLContentContext() : base("XBLToolsDB")
{
}
}
我使用 XBLContent 作为我的主要业务对象,也许这不是最好的主意。我认为我设计的架构有问题,因为我无法将信息发送到视图并过滤、排序等。
现在,我正在使用 Telerik 网格,当我尝试按导航属性字段排序时,我遇到了 问题收到错误消息“不存在属性或字段”。也许我不应该使用 XBLContent 作为我的主要业务对象,或者创建一个包含所有需要的字段的 ViewModel 并将其发送到视图。或者创建一个拆分为两个 EF 表的单个实体(我不知道这是否可能或如何实现)。
我只是 .NET 的学徒,需要一些绝地大师的建议。
我需要可以有多种翻译的内容。
如何最好地实现这一目标?
Using VS2010, .NET4.0, MVC3, EF4.1 Code-First
I have this POCO entities:
public class XBLContent
{
[Key]
[StringLength(36, ErrorMessage="Must have 36 characters")]
[Required(ErrorMessage="Must have a unique GUID")]
public string GUID { get; set; }
public int Price { get; set; }
public float FileSize { get; set; }
public virtual ICollection<XBLRegionalContent> RegionalInfo { get; set; }
public string RelatedGameId { get; set; }
[ForeignKey("RelatedGameId")]
public virtual XBLContent RelatedGame { get; set; }
}
public class XBLRegionalContent
{
[Key, Column(Order = 0)]
public string ContentId { get; set; }
[ForeignKey("ContentId")]
public virtual XBLContent Content { get; set; }
[Key, Column(Order = 1)]
public string RegionId { get; set; }
[ForeignKey("RegionId")]
public virtual XBLRegion Region { get; set; }
public string Name { get; set; }
}
public class XBLRegion
{
[Key]
[StringLength(5, ErrorMessage="ID must have 5 characters")]
[Required]
[RegularExpression(@"[a-z|A-Z]{2}-[A-Z|a-z]{2}")]
public string ID { get; set; }
public string Country { get; set; }
public string Language { get; set; }
}
Relationships:
- One XBLContent has many XBLRegionalContent;
- One XBLContent can be related to another XBLContent(most of them are not);
- One XBLRegionalContent has one XBLContent and one XBLRegion;
- One XBLRegion has many XBLRegionalContent;
The Context objetc is really simple:
public class XBLContentContext : DbContext
{
public DbSet<XBLContent> XBLContents { get; set; }
public DbSet<XBLRegionalContent> XBLRegionalInfos { get; set; }
public DbSet<XBLRegion> XBLRegion { get; set; }
public XBLContentContext() : base("XBLToolsDB")
{
}
}
I'm using XBLContent as my main business object and maybe that is not the best idea. I think there is something wrong with the architecture I designed because I'm having trouble to send information to the View and filter, sort, etc.
Now, I'm using Telerik grid and when I try to sort by a navigation property field I get an error saying that "No property or field exist". Maybe I should not use XBLContent as my main business object, or create a ViewModel containing all needed fields and send it to the View. Or create one single entity that splits into two EF tables(I don't know if that is possible or how to achieve that).
I'm just padawan in .NET and need some Jedi Masters advice.
I need contents that can have multiple translations.
How to best achieve this goal?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我假设您在这里使用 Telerik MVC 扩展,但如果您使用不同的产品,请告诉我,我将相应地重新回答:)
关于网格,您使用哪种绑定?如果您使用常规服务器或ajax绑定,那么在绑定到导航属性时可能会遇到一些问题,因为默认情况下这些绑定仅适用于原始(int、string等)类型。但是,有一种自定义绑定允许您完全控制分页/排序/过滤。我相信这可以解释您收到此错误的原因,因为自动 LINQ 表达式无法找到您正在查找的特定字段。这里有两个演示(其中包含 WebForms 和 Razor ViewEngines 的源代码),可以帮助设置自定义绑定。它只是比自动绑定多了一点工作,但应该仍然可以工作(请注意,这些示例使用 Razor)
这里的额外好处是您可以自己控制一切,这在更复杂的场景中会非常好。如果您已经在使用自定义绑定,和/或那里的链接没有帮助,请告诉我。拥有 Telerik Grid 的代码也可能会有所帮助。
I'm assuming you're using the Telerik MVC Extensions here, but if you are using a different product please let me know and I'll re-answer accordingly :)
In regards to the Grid what kind of binding are you utilizing? If you are using regular server or ajax binding then you might run into some issues when binding to a navigational property, as by default these bindings only work with primitive (int, string etc.) types. However, there is such a thing as custom binding which allows you to take full control over paging/sorting/filtering. I believe this could account for why you are getting this error, as the automatic LINQ expressions cannot find the specific field you are looking for. Here are two demos (which have source code for both WebForms and Razor ViewEngines) that can help with setting up custom binding. It's just a little more work than the automatic binding, but should still work (note that these examples are using Razor):
The added benefit here is that you get to control everything on your own, which can be quite nice in somewhat more complex scenarios. If you're already using custom binding, and/or if the links there do not help let me know. It could also be helpful to have the code for the Telerik Grid.
我通过规范化结果解决了此类问题,例如:
本质上创建一个更加非规范化的匿名类对我来说可以解决此类问题,其中结果也使这些导航属性非规范化。
HTH。
I've resolved these kinds of issues by normalizing the results like:
Essentially creating an anonymous classes that is more denormalized has worked for me to work around these kinds of issues, where the results denormalizes those navigational properties too.
HTH.
这应该可以解决你的问题。
http://weblogs.asp.net/manavi/ 对于初学者来说是一个很好的资源,我可以看到你使用了很多注释,所以一点流畅的 api 会让你的概念更强大。
this should fix your problem.
http://weblogs.asp.net/manavi/ A great resource for beginners and i can see you have used a lot of annotations ,so a little bit of fluent api would make your concepts stronger.