开发者

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

开发者 https://www.devze.com 2023-02-11 11:51 出处:网络
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?

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.Prope开发者_如何学编程rties(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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号