ErocM
ErocM

Reputation: 4662

adding a view to my model and it is requiring a key

I am attempting to add a view to my model and it is giving me errors.

Here is the message it is giving me when I first try to add the view:

The model was generated with warnings or errors.SuburbanPortalModel.edmxPlease see the Error List for more details. These issues must be fixed before running your application.
Loading metadata from the database took 00:00:02.4055669.
Generating the model took 00:00:04.5390309.

I checked the errors/warnings and it showed this:

Warning 1   Error 6013: The table/view 'SuburbanPortal.Web.MembershipUsers' does not have a primary key defined and no valid primary key could be inferred. This table/view has been excluded. To use the entity, you will need to review your schema, add the correct keys, and uncomment it.  D:\Suburban\Source3\Web Projects\WebUsers\WebUsers\SuburbanPortalModel.edmx 1   1   WebUsers

I have other views added and it didn't give me an issue:

Warning 2   Error 6002: The table/view 'SuburbanPortal.Web.vw_WebCustomer' does not have a primary key defined. The key has been inferred and the definition was created as a read-only table/view. D:\Suburban\Source3\Web Projects\WebUsers\WebUsers\SuburbanPortalModel.edmx 1   1   WebUsers

Notice the key had been inferred here but not the previous view.

This is how I am adding my view:

enter image description here

I didn't think you could add a key to a view? I'm not sure what I am doing wrong, can anyone point me in the right directions?

EDIT#1

This is what makes up my view:

SELECT        dbo.aspnet_Membership.ApplicationId, dbo.aspnet_Membership.UserId, dbo.aspnet_Membership.Password, dbo.aspnet_Membership.PasswordFormat, dbo.aspnet_Membership.PasswordSalt, 
                         dbo.aspnet_Membership.MobilePIN, dbo.aspnet_Membership.Email, dbo.aspnet_Membership.LoweredEmail, dbo.aspnet_Membership.PasswordQuestion, dbo.aspnet_Membership.PasswordAnswer, 
                         dbo.aspnet_Membership.IsApproved, dbo.aspnet_Membership.IsLockedOut, dbo.aspnet_Membership.CreateDate, dbo.aspnet_Membership.LastLoginDate, dbo.aspnet_Membership.LastPasswordChangedDate, 
                         dbo.aspnet_Membership.LastLockoutDate, dbo.aspnet_Membership.FailedPasswordAttemptCount, dbo.aspnet_Membership.FailedPasswordAttemptWindowStart, 
                         dbo.aspnet_Membership.FailedPasswordAnswerAttemptCount, dbo.aspnet_Membership.FailedPasswordAnswerAttemptWindowStart, dbo.aspnet_Membership.Comment, dbo.aspnet_Users.UserName, 
                         dbo.aspnet_Users.LoweredUserName, dbo.UsersAccountLink.TokenId, dbo.UsersAccountLink.UsersLinkId, dbo.UsersAccountLink.IsActive, dbo.aspnet_Users.LastActivityDate, dbo.UsersAccountLink.AccountId, 
                         dbo.aspnet_Applications.ApplicationName, dbo.aspnet_Applications.LoweredApplicationName, dbo.aspnet_Applications.Description, Web.vw_WebCustomer.Branch, Web.vw_WebCustomer.AccountNumber, 
                         Web.vw_WebCustomer.CorporationId, Web.vw_WebCustomer.Name, Web.vw_WebCustomer.Street, Web.vw_WebCustomer.City, Web.vw_WebCustomer.State, Web.vw_WebCustomer.ZipCode, 
                         Web.vw_WebCustomer.AreaCode, Web.vw_WebCustomer.PhoneNumber, Web.vw_WebCustomer.CareOf, Company.Corporation.Name AS CompanyName, Company.Corporation.CompanyCode
FROM            Company.Corporation RIGHT OUTER JOIN
                         Web.vw_WebCustomer ON Company.Corporation.CorporationId = Web.vw_WebCustomer.CorporationId RIGHT OUTER JOIN
                         dbo.UsersAccountLink ON Web.vw_WebCustomer.AccountId = dbo.UsersAccountLink.AccountId RIGHT OUTER JOIN
                         dbo.aspnet_Membership ON dbo.UsersAccountLink.UserId = dbo.aspnet_Membership.UserId LEFT OUTER JOIN
                         dbo.aspnet_Users ON dbo.UsersAccountLink.UserId = dbo.aspnet_Users.UserId AND dbo.aspnet_Membership.UserId = dbo.aspnet_Users.UserId FULL OUTER JOIN
                         dbo.aspnet_Applications ON dbo.aspnet_Membership.ApplicationId = dbo.aspnet_Applications.ApplicationId AND dbo.aspnet_Users.ApplicationId = dbo.aspnet_Applications.ApplicationId

======= ANSWER ======

Per simon at rcl:

EF wants each table it has to provide a unique identifier and is unhappy if there isn't one.

I should have listened to his initial response.

I was thinking this was a model issue and not a query issue since I was getting the message when I tried to update my Model.

For those looking for a solution in the future, it isn't an issue with the Model. It is looking at your View and not seeing anything it can create a unique key on. Check your view and make sure it is returning results that are unique. In my case, I had to rework the view to give me unique results. I wanted unique results and expected unique results... but my view wasn't returning them since I did not set it up correctly.

If you do not have any unique fields for Model to assume, you will have to create a field with a unique key on your view to give it a unique key to build on.

Upvotes: 0

Views: 3732

Answers (1)

simon at rcl
simon at rcl

Reputation: 7344

In the first, it's saying it can't work out what the Primary (unique) key might be, so it's excluding it. In the second it's saying that it can work it out so it's keeping in.

After updating the Model, select the imported view, For each column which make up the unique identifier of a row in the view, select the column and then in the Properties set Entity Key to True. Then compile.

EF wants each table it has to provide a unique identifier and is unhappy if there isn't one. This doesn't affect the database. For a view called MembershipUsers, I imaging that there might be an Id or a Logon or EMail address which should be unique.

Upvotes: 1

Related Questions