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 6026901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:30:45+00:00 2026-05-23T04:30:45+00:00

Using the following classes by convention EF will create the tables and PK/FK associations

  • 0

Using the following classes by convention EF will create the tables and PK/FK associations correctly. However when i add data the to photos collection and save the entities the data in the Photos table is not saved to the database table.

I’ m avoiding using any collection types which inherits from ICollection for the public property and instead used a private backing field as i want to control access to Add/Remove methods. Is there anything additional i need to add to the OnModelCreating to tell EF that there’s data in IEnumerable Photos and persist it?

(Data from the Album class can be saved correctly)

public class Album
{
    private readonly ICollection<Photo> _photos = new List<Photo>();
    public Guid Id {get; set;}      
    public string Name {get; set;}
    public virtual IEnumerable<Photo> Photos
    {
        get{ return _photos;}
    }

    public void AddPhoto(byte[] bytes, string name)
    {
        //some biz rules
        Photo p = new Photo();
        p.Bytes = bytes;
        p.Name = name;
        _photos.Add(p);
    }
}

public class Photo
{
    public Guid Id {get; set;}
    public string Name {get; set;}
    public byte[] Bytes {get; set;}
}

public class AlbumDbContext : DbContext
{
    public AlbumDbContext()
    {
        this.Database.CreateIfNotExists();
    }

    public DbSet<Album> Albums { get; set; }        


    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {              
    }
}
  • 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-23T04:30:46+00:00Added an answer on May 23, 2026 at 4:30 am

    I even wonder that tables and relations are correctly created especially when none of your entities have defined key and context doesn’t define set for Photos. Anyway ICollection<T> is a must. Your class is not valid entity and even if you somehow make it work I expect that you will have troubles to use it – for example you can forget about lazy loading.

    Also handling it this way doesn’t make sense because anybody can convert your enumerable back to collection. You must make enumerable copy of the collection to make it work as supposed.

    The only way which can perhaps work is:

    public class Album
    {
        public class AlbumConfiguration : EntityTypeConfiguration<Album> 
        {
            public AlbumConfiguration()
            {
                // Inner class will see private members of the outer class
                HasMany(a => a._photos)...
            }
        }
    
        private readonly ICollection<Photo> _photos = new List<Photo>();
        public string Name {get; set;}
        public virtual IEnumerable<Photo> Photos
        {
            get{ return _photos;}
        }
    
        public void AddPhoto(byte[] bytes, string name)
        {
            //some biz rules
            Photo p = new Photo();
            p.Bytes = bytes;
            p.Name = name;
            _photos.Add(p);
        }
    }
    

    And your context will look like:

    public class AlbumDbContext : DbContext
    {
        public DbSet<Album> Albums { get; set; }        
        public DbSet<Photo> Photos { get; set; }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {    
            modelBuilder.Configurations.Add(new Album.AlbumConfiguration());          
        }
    }
    

    This solution is pretty ugly because it makes your entity dependent on entity framework.

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

Sidebar

Related Questions

I've been using the following code in my Web form code-behind classes. I want
I have the following classes: using System; using System.Windows.Forms; namespace FastEyeControl { public partial
Using the following classes.. public class Trait { public virtual int Id { get;
Using following code I try to get updated list of checkbuttons' corresponding text values,
I using following code: var search = 'test'; if ($('#sku').find(search) ){ //alert(search); $(document).find(search).css('color','red'); <TABLE>
I am using following code to handle all the unhandled exceptions in the program.
I am using following code to remove some specific nodes from xml file..It show
I am using following PHP code to connect to MS Access database: $odb_conn =
I am using following storedprocedure set ANSI_NULLS ON set QUOTED_IDENTIFIER ON GO Alter PROCEDURE
I am using following code in rowdatabound function. Protected Sub gvwMileStone_RowDataBound(ByVal sender As System.Object,

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.