Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update grandchild, where child is linktable #160

Open
Jogai opened this issue May 12, 2016 · 0 comments
Open

Update grandchild, where child is linktable #160

Jogai opened this issue May 12, 2016 · 0 comments

Comments

@Jogai
Copy link

Jogai commented May 12, 2016

I'm trying to update a graph where the child is just for linking to the grandchild, however the child is inserted with only NULL values leaving the grandchild unlinked. How can I solve this?

My code

                _db.UpdateGraph(sector, up => up
                    .OwnedEntity(s => s.Image, date => date
                        .OwnedEntity(i => i.Picture))
                    );

Executed SQL:

SET SESSION sql_mode='ANSI';INSERT INTO `Images`(
`PictureId`, 
`PhotoId`) VALUES (
NULL, 
NULL);
SELECT
`Id`
FROM `Images`
WHERE  row_count() > 0 AND `Id`=last_insert_id();
SET SESSION sql_mode='ANSI';INSERT INTO `Pictures`(
`Logo`, 
`Path`, 
`Description`, 
`Created`) VALUES (
0, 
'/User_Data/Misc/2016/5/12/Untitled.gif' /* @gp1 */, 
NULL, 
5/12/2016 9:05:13 AM /* @gp2 */);
SELECT
`Id`
FROM `Pictures`
WHERE  row_count() > 0 AND `Id`=last_insert_id()

My models:

    public class Sector
    {
        public int Id { get; set; }

        public string Name { get; set; }

        public bool Active { get; set; }

        public int? ImageId { get; set; }

        public virtual Image Image { get; set; }
    }
    public class Image
    {
        public int Id { get; set; }

        public int? PictureId { get; set; }

        public Picture Picture { get; set; }

        public int? PhotoId { get; set; }

        public Photo Photo { get; set; }

        public virtual ICollection<Sector> Sectors { get; set; }
    }
    public class Picture
    {
        public int Id { get; set; }

        public string Path { get; set; }

        public string Description { get; set; }

        public DateTime Created { get; set; }
    }

Code first configuration:

    public class ImageEntityConfig : EntityTypeConfiguration<Image>
    {
        public ImageEntityConfig()
        {
            HasOptional(i => i.Picture)
                .WithMany()
                .HasForeignKey(i => i.PictureId);
            HasOptional(i => i.Photo)
                .WithMany()
                .HasForeignKey(i => i.PhotoId);
        }
    }
Jogai referenced this issue in Kyle2123/GraphDiff May 12, 2016
…gation property and then calling UpdateGraph() and then saving to database would result in FK ID change not being persisted.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant