开发者

Entity-splitting scenario in EF4 CTP5 code-first implementation

开发者 https://www.devze.com 2023-02-17 06:51 出处:网络
I think what I am looking for is actually entity-splitting. However, I am not 100% sure if the way I need to do it is supported. So, I will add a great deal of detail hear that will hopefully help. I

I think what I am looking for is actually entity-splitting. However, I am not 100% sure if the way I need to do it is supported. So, I will add a great deal of detail hear that will hopefully help. I will limit the scope to one entity since the answer will be the same across all of those I have to deal with.

So, I have a User entity:

User
ID (int)
CustID (int)
CustomerString (strin开发者_运维百科g)
FirstName (string)
LastName (string)
Email (string)

Due to the multi-tenancy of our database each object has the concept of an owner tied to it. This is represented by a CustID in all tables. However, this id is a meaningless PK to our Customers that will be accessing this Data Service. They know their CustomerString which is just a unique string value that equates to the ID in our Client table.

The two tables related to the User entity are as such:

Customers

CREATE TABLE [Customers](
 [ID] [int] NOT NULL,
 [Name] [varchar](100) NOT NULL,
 [Description] [varchar](255) NULL,
 [ClientPhone] [varchar](20) NULL,
 [Address] [varchar](255) NULL,
 [CustomerString] [varchar](100) NOT NULL,
 [sys_CreateDate] [datetime] NOT NULL,
 [sys_LastUpdate] [datetime] NOT NULL,
 CONSTRAINT [PK_Customers] PRIMARY KEY NONCLUSTERED 
(
 [ID] ASC
)

Users

CREATE TABLE [Users](
 [UserID] [int] IDENTITY(2000000,1) NOT NULL,
 [CustID] [int] NOT NULL,
 [FirstName] [varchar](40) NOT NULL,
 [LastName] [varchar](40) NOT NULL,
 [mail] [varchar](255) NULL,
 [CreateDate] [datetime] NOT NULL,
 [ModifyDate] [datetime] NOT NULL,
 CONSTRAINT [PK2_Users] PRIMARY KEY CLUSTERED 
(
 [UserID] ASC
)

So, currently, I have the User entity mapped to the Users table as follows:

ID  ==> Users.UserID
CustID ==> Users.CustID
CustomerString <NOT MAPPED>
FirstName ==> Users.FirstName
LastName ==> Users.LastName
Email ==> Users.Mail

Now, here comes my sticking-point. I need to map the CustomerString to Customers.CustomerString where Users.CustID = Customers.ID. It appears to me, from what I have read, that this would not be a problem if the IDs were named the same in each table. However, as you can see, this is not the case.

Please help! This is an absolute requirement for this project that I have been working on the past month or so.

Thank you in advance for any help you could provide!

Ryan


I think it is not possible. Fist problem is that EF is not able to use unique keys (will be probably available in next major version). So you can't enforce 1:1 relation between Customer->Id and User->CustId (which requires unique key constraint). To enable 1:1 relation in EF, entities must "share" PK. It means that you can create 1:1 relation only on Customer->Id and User->Id which you obviously don't want.

Second problem is that resulting User class is not an entity. It doesn't contain all columns from both classes. It is a projection of these two entities. You can use these two entities and create custom linq query to get projection of to this readonly class like:

var query = from u in context.Users
            join c in context.Customers on u.CustId equals c.Id
            select new UserProjection
              {
                Id = u.Id,
                CustId = c.Id,
                CustomerString = c.CustomerString,
                FirstName = u.FistName,
                LastName = u.LastName
                Email = u.Mail
              }; 

Also be aware that CTP5 is already obsolete. EF 4.1 RC was relased two days ago.

0

精彩评论

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

关注公众号