Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 755
Col Avatar asked Feb 17 '11 16:02

Col


1 Answers

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.

like image 191
Morteza Manavi Avatar answered Sep 30 '22 16:09

Morteza Manavi