Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 731579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:02:35+00:00 2026-05-14T07:02:35+00:00

For my ASP.NET MVC 2 application I use Entity Framework 1.0 as my data

  • 0

For my ASP.NET MVC 2 application I use Entity Framework 1.0 as my data access layer (repository). But I decided I want to return POCO. For the first time I have encountered a problem when I wanted to get a list of Brands with their optional logos. Here’s what I did:

public IQueryable<Model.Products.Brand> GetAll()
    {
        IQueryable<Model.Products.Brand> brands = from b in EntitiesCtx.Brands.Include("Logo")
                                                  select new Model.Products.Brand()
                                                         {
                                                             BrandId = b.BrandId,
                                                             Name = b.Name,
                                                             Description = b.Description,
                                                             IsActive = b.IsActive,
                                                             Logo = /*b.Logo != null ? */new Model.Cms.Image()
                                                                    {
                                                                        ImageId = b.Logo.ImageId,
                                                                        Alt = b.Logo.Alt,
                                                                        Url = b.Logo.Url
                                                                    }/* : null*/
                                                         };
        return brands;
    }

You can see in the comments what I would like to achieve. It worked fine whenever a Brand had a Logo otherwise it through an exception that you can assign null to the non-nullable type int (for Id). My workaround was to use nullable in the POCO class but that’s not natural – then I have to check not only if Logo is null in my Service layer or Controllers and Views but mostly for Logo.ImageId.HasValue. It’s not justified to have a non null Logo property if the id is null.

Can anyone think of a better solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T07:02:35+00:00Added an answer on May 14, 2026 at 7:02 am

    I have another workaround for this. Since in some cases the Image (the class for Logo property) can’t be null and in some it can I decided to add some inheritance to my model of the Image. Here’s what I’ve done:

    public class OptionalImage
    {
        public long? ImageId
        {
            get;
            set;
        }
    
        [DisplayName("Obraz")]
        [StringLength(200, ErrorMessage = "Url jest za długi")]
        [RegularExpression(@".*\.(jpg|gif|png|jpeg|tif|tiff|JPG|GIF|PNG|JPEG|TIF|TIFF)$", ErrorMessage = "Rozszerzenie pliku jest nieprawidłowe. Dopuszczone to: .jpg, .gif, .png, .jpeg, .tif, .tiff")]
        public string Url
        {
            get;
            set;
        }
    
        [StringLength(200, ErrorMessage = "Tekst alternatywny jest za długi")]
        [DisplayName("Tekst alternatywny")]
        public string Alt
        {
            get;
            set;
        }
    }
    
    public class Image : OptionalImage
    {
        public new long ImageId
        {
            get;
            set;
        }
    }
    

    Then my repository returns a nullable Id only for the objects that have 0..1 relation with Image – like Brand. But a Product which has a required DefaultImage property will use Image without nullable Id which is corresponding to the database and business logic design.

    The GetAll method in the repository now looks like this (thanks @omoto for the tip):

    public IQueryable<Model.Products.Brand> GetAll()
        {
            IQueryable<Model.Products.Brand> brands = from b in EntitiesCtx.Brands
                                                      let logo = EntitiesCtx.Images.FirstOrDefault(c => c.ImageId == b.Logo.ImageId)
                                                      select new Model.Products.Brand()
                                                             {
                                                                 BrandId = b.BrandId,
                                                                 Name = b.Name,
                                                                 Description = b.Description,
                                                                 IsActive = b.IsActive,
                                                                 Logo = new Model.Cms.OptionalImage()
                                                                        {
                                                                            ImageId = logo.ImageId,
                                                                            Alt = logo.Alt,
                                                                            Url = logo.Url
                                                                        }
                                                             };
            return brands;
        }
    

    For products instead of new Model.Cms.OptionalImage() I will use new Model.Cms.Image() and since the value of ImageId database filed in that case can’t be null it will work fine and everything will be natural in the controllers and views.
    I think this kind of workaround suits my needs very well. Still if anyone has a better solution feel free to reply.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 398k
  • Answers 398k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Consider the Auto Assign Role module. Here is a quote… May 15, 2026 at 3:40 am
  • Editorial Team
    Editorial Team added an answer Looks okay to me. It looks like you're using a… May 15, 2026 at 3:40 am
  • Editorial Team
    Editorial Team added an answer You'll have to set a reference to ADO and then… May 15, 2026 at 3:40 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.