添加一个视图到我的模型,它需要一个键

时间:2014-06-10 14:34:40

标签: c# entity-framework sql-server-2008-r2

我正在尝试为我的模型添加一个视图,它给了我错误。

以下是我第一次尝试添加视图时给出的消息:

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.

我检查了错误/警告并显示了这个:

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

我添加了其他观点,并没有给我一个问题:

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

请注意,此处已推断出密钥,但不是之前的视图。

这就是我添加观点的方式:

enter image description here

我认为你不能为视图添加密钥?我不确定我做错了什么,有人能指出我正确的方向吗?

修改#1

这就构成了我的观点:

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

=======答案======

在rcl的每个西蒙:

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

我应该听听他最初的回应。

我认为这是一个模型问题而不是查询问题,因为我在尝试更新模型时收到了消息。

对于那些寻求未来解决方案的人来说,这不是模型的问题。它正在查看您的视图,而不会看到任何可以创建唯一键的内容。检查您的视图并确保它返回唯一的结果。就我而言,我不得不重新设计视图以给我独特的结果。我想要独特的结果和预期的独特结果...但我的观点并没有归还它们,因为我没有正确设置它。

如果您没有要为Model假设的任何唯一字段,则必须在视图上创建一个具有唯一键的字段,以便为其提供唯一的密钥来构建。

1 个答案:

答案 0 :(得分:1)

首先,它说它无法解决主要(唯一)密钥的问题,因此将其排除在外。在第二个中它说它可以解决它所以它保持。

更新模型后,选择导入的视图,对于构成视图中行的唯一标识符的每个列,选择该列,然后在属性中将实体键设置为True。然后编译。

EF希望它拥有的每张桌子都提供一个唯一的标识符,如果没有,则不满意。这不会影响数据库。对于名为MembershipUsers的视图,我想象可能存在唯一的Id或登录或电子邮件地址。