Entity Framework CTP5 Code First - Possible to do entity splitting on a non-primary key?
Asked Answered
L

1

6

Using EF CTP5, I am trying to do some entity splitting where the entity is constructed from two separate tables. Is it possible to do this splitting if the key on the two tables is not the primary key?

E.g. Id is my primary key on the Note entity. I want to get my CreatedUser details from a separate table but the primary key on this second table corresponds to CreatedUserId in the Note entity.

        modelBuilder.Entity<Note>()
            .Map(mc =>
            {
                mc.Properties(n => new
                {
                    n.Id,
                    n.Title,
                    n.Detail,
                    n.CreatedUserId,
                    n.CreatedDateTime,
                    n.UpdatedUserId,
                    n.UpdatedDateTime,
                    n.Deleted,
                    n.SourceSystemId,
                    n.SourceSubSystemId
                });
                mc.ToTable("Notes");
            })
            .Map(mc =>
            {
                mc.Properties(n => new
                {
                    n.CreatedUserId,
                    n.CreatedUser
                });
                mc.ToTable("vwUsers");
            });

I've seen comments that entity splitting is only possible if the entity primary key exists in both tables?

Thanks in advance.

League answered 17/2, 2011 at 16:38 Comment(0)
R
2

Yes, all the tables that are being generated in an entity splitting scenario must have the object identifier (e.g. Note.Id) as their primary key. You should consider creating a 1:* association between User and Note entities in this case.

Retrorse answered 17/2, 2011 at 17:25 Comment(1)
Thanks for confirming this Morteza and the quick response. I'll move onto implementing the 1.* tomorrow!League

© 2022 - 2024 — McMap. All rights reserved.