Click here to Skip to main content
15,944,832 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
according to the code in what I have tried section
BuySellHouse is parent
EstateImages is child
I tried to create a 1 to 0/1 relationship. I am not sure I did it correctly
inserting parent with child by code like this gives me an error

C#
BuySellHouse parent =new BuySellHouse();
EstateImages child=new EstateImages();
parent.EstateImages =child;
mydb.BuySellHouse.Add(parent);


the error
Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values

What I have tried:

C#
public class BuySellHouse
    {
        public int Id { get; set; }
//other fields

        public int? EstateImagesId { get; set; }
        public  EstateImages EstateImages { get; set; }
    }

public class EstateImages
    {
        public int Id { get; set; }
//other fields

        public int BuySellHouseId { get; set; }
        public  BuySellHouse BuySellHouse { get; set; }
    }


 public  class BuySellHouseConfig : EntityTypeConfiguration<BuySellHouse>
    {
        public BuySellHouseConfig()
        {
            HasKey(x => x.Id);
            Property(x => 
            x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
          
            ToTable("BuySellHouse");

           HasOptional(x => x.EstateImages).WithMany().HasForeignKey(x => 
           x.EstateImagesId).WillCascadeOnDelete(false);
        }
    }
public  class EstateImagesConfig : EntityTypeConfiguration<EstateImages>
    {
        public EstateImagesConfig()
        {
            HasKey(x => x.Id);
            Property(x => 
            x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

            ToTable("EstateImages");
            HasRequired(x => x.BuySellHouse).WithMany().HasForeignKey(x => 
            x.BuySellHouseId).WillCascadeOnDelete(true);
        }
    }
Posted
Updated 2-Dec-21 22:38pm
v2

1 solution

You've created both a one-to-one relationship (by convention) and two one-to-many relationships (by configuration). That's definitely not correct.

Based on your description, you need:
C#
public class BuySellHouse
{
    public int Id { get; set; }
    //other fields
    
    public int? EstateImagesId { get; set; }
    public EstateImages EstateImages { get; set; }
}

public class EstateImages
{
    public int Id { get; set; }
    //other fields

    public int BuySellHouseId { get; set; }
    public  BuySellHouse BuySellHouse { get; set; }
}


public  class BuySellHouseConfig : EntityTypeConfiguration<BuySellHouse>
{
    public BuySellHouseConfig()
    {
        HasKey(x => x.Id);
        Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        ToTable("BuySellHouse");
    }
}

public  class EstateImagesConfig : EntityTypeConfiguration<EstateImages>
{
    public EstateImagesConfig()
    {
        HasKey(x => x.Id);
        Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        ToTable("EstateImages");
        
        HasRequired(x => x.BuySellHouse).WithOptional(x => x.EstateImages).WillCascadeOnDelete(true);
    }
}
Fluent API - Relationships - EF6 | Microsoft Docs[^]
 
Share this answer
 
v2
Comments
4L4K1 3-Dec-21 5:32am    
I want 1 to 0/1 relationship
Richard Deeming 3-Dec-21 6:02am    
I've updated my answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900