开发者

Entities Architecture

开发者 https://www.devze.com 2023-04-04 07:13 出处:网络
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\")]

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?


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.


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):

  • Ajax Binding
  • Server Binding

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.


I've resolved these kinds of issues by normalizing the results like:

from r in ctx.XBLContents
select new
{
   r.Guid,
   RelatedGuid = r.RelatedGame.Guid
};

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消