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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T16:06:52+00:00 2026-06-18T16:06:52+00:00

I use Code First EF 5.0 on >Net 4.0 and I has 2 class:

  • 0

I use Code First EF 5.0 on >Net 4.0 and I has 2 class:

public partial class Kennel
{
    public Kennel()
    {
        this.Brands = new List<Brand>();
        this.Dogs = new List<Dog>();
        this.Breeds = new List<Breed>();
        this.Owners = new List<Person>();
        this.Name1 = new KennelName();
        this.Name2 = new KennelName();
    }

    public int ID { get; set; }
    public /*DogClub*/int Type { get; set; }
    public KennelName Name1 { get; set; }
    public KennelName Name2 { get; set; }
    public string CertificateNumber { get; set; }
    public System.DateTime? AssigmentDate { get; set; }
    public string Folder { get; set; }
    public string Comment { get; set; }
    public int StatusID { get; set; }
    public int? FederationID { get; set; }
    public int? MainOwnerID { get; set; }
    public int? MainBreedID { get; set; }

    public virtual ICollection<Brand> Brands { get; set; }
    public virtual ICollection<Dog> Dogs { get; set; }
    public virtual Breed MainBreed { get; set; }
    public virtual Federation Federation { get; set; }
    public virtual Status Status { get; set; }
    public virtual Person MainOwner { get; set; }
    public virtual ICollection<Breed> Breeds { get; set; }
    public virtual ICollection<Person> Owners { get; set; }
}

public partial class Breed
{
    public Breed()
    {
        this.Dogs = new List<Dog>();
        this.ExpertKerungs = new List<ExpertKerung>();
        this.Hallmarks = new List<Hallmark>();
        this.Colors = new List<Color>();
        this.ExpertBreeds = new List<ExpertBreed>();
        this.Kennels = new List<Kennel>();
        this.MainKennels = new List<Kennel>();
    }

    public int ID { get; set; }
    public string FciNumber { get; set; }
    public string Name { get; set; }
    public int BreedGroupID { get; set; }
    public bool IsKerung { get; set; }
    public string NameLat { get; set; }
    public string NativeName { get; set; }
    public int CountryID { get; set; }
    public System.DateTime? StandardDate { get; set; }
    public bool IsCACIB { get; set; }
    public bool IsWork { get; set; }

    public virtual BreedGroup BreedGroup { get; set; }
    public virtual ICollection<Dog> Dogs { get; set; }
    public virtual ICollection<ExpertKerung> ExpertKerungs { get; set; }
    public virtual ICollection<Hallmark> Hallmarks { get; set; }
    public virtual ICollection<Color> Colors { get; set; }
    public virtual Country Country { get; set; }
    public virtual ICollection<ExpertBreed> ExpertBreeds { get; set; }
    public virtual ICollection<Kennel> Kennels { get; set; }
    public virtual ICollection<Kennel> MainKennels { get; set; }
}

and mapping:

public class KennelMap : EntityTypeConfiguration<Kennel>
{
    public KennelMap()
    {
        // Primary Key
        this.HasKey(t => t.ID);

        // Properties
        //this.Property(t => t.Name1.Name)
        //    .IsRequired();

        //this.Property(t => t.Name1.IntlName)
        //    .IsRequired();

        //this.Property(t => t.Name2.Name)
        //    .IsRequired();

        //this.Property(t => t.Name2.IntlName)
        //    .IsRequired();

        // Table & Column Mappings
        this.ToTable("Kennels");
        this.Property(t => t.ID).HasColumnName("ID");
        this.Property(t => t.Type).HasColumnName("Type");
        this.Property(t => t.Name1.Name).HasColumnName("Name1_Name");
        this.Property(t => t.Name1.IntlName).HasColumnName("Name1_IntlName");
        this.Property(t => t.Name1.Type).HasColumnName("Name1_Type");
        this.Property(t => t.Name1.Approved).HasColumnName("Name1_Approved");
        this.Property(t => t.Name2.Name).HasColumnName("Name2_Name");
        this.Property(t => t.Name2.IntlName).HasColumnName("Name2_IntlName");
        this.Property(t => t.Name2.Type).HasColumnName("Name2_Type");
        this.Property(t => t.Name2.Approved).HasColumnName("Name2_Approved");
        this.Property(t => t.CertificateNumber).HasColumnName("CertificateNumber");
        this.Property(t => t.AssigmentDate).HasColumnName("AssigmentDate");
        this.Property(t => t.Folder).HasColumnName("Folder");
        this.Property(t => t.Comment).HasColumnName("Comment");
        this.Property(t => t.StatusID).HasColumnName("StatusID");
        this.Property(t => t.FederationID).HasColumnName("FederationID");
        this.Property(t => t.MainOwnerID).HasColumnName("MainOwnerID");

        // Relationships
        this.HasMany(t => t.Owners)
                .WithMany(t => t.Kennels)
                .Map(m =>
                        {
                            m.ToTable("OwnerKennel");
                            m.MapLeftKey("Kennels_ID");
                            m.MapRightKey("Owners_ID");
                        });

        this.HasOptional(t => t.MainBreed)
                .WithMany(t => t.MainKennels)
                .HasForeignKey(d => d.MainBreedID);
        this.HasOptional(t => t.Federation)
                .WithMany(t => t.Kennels)
                .HasForeignKey(d => d.FederationID);
        this.HasRequired(t => t.Status)
                .WithMany(t => t.Kennels)
                .HasForeignKey(d => d.StatusID);
        this.HasOptional(t => t.MainOwner)
                .WithMany(t => t.MainKennels)
                .HasForeignKey(d => d.MainOwnerID)
                .WillCascadeOnDelete(false);

    }
}

If I write next code:

  int breedID = 1; // some value  
  Breed br = _kennel.Breeds.FirstOrDefault(t => t.ID == breedID);  
  if (br != null)  
  {  
    _kennel.MainBreed = br;  
    // but: _kennel.MainBreedID != br.ID  
  }  

OR:

  int breedID = 1; // some value  
  Breed br = _kennel.Breeds.FirstOrDefault(t => t.ID == breedID);  
  if (br != null)  
  {  
    _kennel.MainBreedID = breedID;  
    // but: _kennel.MainBreed != br  
  }  

Why EF doesnt update navigation property? I set ProxyCreationEnabled and AutoDetectChangesEnabled, but this not work.

See another example of sample code (it accurately reflects my real application code):

Kennel kennel = ctx.Kennels.Add(ctx.Kennels.Create());
kennel.Name1.Name = "Test Kennel";
List<Breed> breeds = ctx.Breeds.Include(b => b.BreedGroup).OrderBy(t => t.BreedGroupID).Where(t => t.ID == 755 || t.ID == 772).ToList();
foreach (var b in breeds)
  kennel.Breeds.Add(b);
if (breeds.Count > 0)
{
  kennel.MainBreed = breeds[0];
  foreach (var k in kennel.MainBreed.MainKennels)
    System.Diagnostics.Debug.WriteLine("MainKennel: " + k.Name1.Name);
  ctx.ChangeTracker.DetectChanges();
  //System.Diagnostics.Debug.WriteLine("MainBreed: " + kennel.MainBreed);
  System.Diagnostics.Debug.WriteLine("MainBreedID: " + kennel.MainBreedID);
}

After call to DetectChanges all navigation properties and collection reflect changes (kennel.MainBreedID != null).

  • 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-06-18T16:06:53+00:00Added an answer on June 18, 2026 at 4:06 pm

    Try making all your POCO properties virtual rather than just the navigation properties. This will allow EF to create change tracking proxies rather than lazy loading proxies. I’ve not tested this, but you may then get the behavior that you expect.

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

Sidebar

Related Questions

Here's sample model classes, which being use with Entity Framework Code First: public class
First off I use this code to make the navigation bar always stay fixed;
I use this code to determine checkbox width and height: var chk = $('input[type=checkbox]:first');
I have an ASP.NET MVC App, which use EF code First, for some reason
I use Entity Framework code-first. DataBaseName - I want to create a database in
I use EF 4.2 code first in my mvc3 project. miniprofiler works fine (sql
I have several Entity Framework Code First DbContext objects that use a custom Initializer.
First the requirements: By management requirements, I can't use open source code. I need
I want to use code beside files for my views in my ASP.NET MVC
I'm using ASP.NET MVC 3 code-first and I have added validation data annotations to

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.