简体   繁体   English

没有定义主键,也无法推断出有效的主键

[英]Does not have a primary key defined and no valid primary key could be inferred

I have this query in making my view but the problem is this view is not included in my Model. 我在查看时遇到此查询,但问题是此视图未包含在我的模型中。 So, I could not use this view. 所以,我无法使用此视图。

Here is my code: 这是我的代码:

CREATE VIEW [dbo].[Payment_Transaction_vw]
AS 
    SELECT 
        id = ROW_NUMBER() OVER (ORDER BY (SELECT NULL)),
        Transaction_Info.trans_name, Student_Info.ID_Number,
        Student_Info.student_fname, Student_Info.student_lname,
        SUM(Payment_Transaction.amount) AS amount
    FROM 
        [Payment_Transaction]
    INNER JOIN 
        Student_Info ON Payment_Transaction.student_info_id = Student_Info.student_info_id 
    INNER JOIN 
        Transaction_Info ON Payment_Transaction.trans_info_id = Transaction_Info.trans_info_id 
    GROUP BY 
        ID_Number, student_lname, student_fname, trans_name;

This view can be seen in the database after I create this. 创建此视图后,可以在数据库中看到此视图。 But not when I add a model. 但是当我添加模型时。 This is not included in my Entity. 这不包括在我的实体中。

Here is the error will displayed: 这是将显示的错误:

dbo.Payment_Transaction_vw does not have a primary key defined and no valid primary key could be inferred. dbo.Payment_Transaction_vw没有定义主键,也无法推断出有效的主键。 This table/view has been excluded. 此表/视图已被排除。 To use the entity, you will need to review our schema, add the correct keys, and uncomment it. 要使用该实体,您需要查看我们的架构,添加正确的密钥并取消注释。

Please help me with this matter. 请帮我解决这个问题。 Thanks in advance. 提前致谢。

I think it might be because EF has a hard time choosing the correct primary key. 我想这可能是因为EF很难选择正确的主键。 A way to force EF to choose a key of your liking seems to be to use ISNULL() for the primary key, and NULLIF() for the other columns (at least the ones that could cause confusion, like primary keys from other tables or non-nullable columns). 强制EF选择你喜欢的密钥的方法似乎是使用ISNULL()作为主键,而NULLIF()用于其他列(至少可能导致混淆的那些,如来自其他表的主键或不可空的列)。

In your case this would mean: 在你的情况下,这将意味着:

SELECT 
    ISNULL(ROW_NUMBER() OVER(ORDER BY (SELECT NULL)), -1) AS Id,
    NULLIF(Transaction_Info.trans_name,'') AS trans_name, 
    NULLIF(Student_Info.ID_Number,-1) AS ID_Number,
    NULLIF(Student_Info.student_fname,'') AS student_fname, 
    NULLIF(Student_Info.student_lname,'') AS student_lname,
    NULLIF(SUM(Payment_Transaction.amount),-1) AS amount
FROM [Payment_Transaction]
INNER JOIN Student_Info ON 
    Payment_Transaction.student_info_id = Student_Info.student_info_id 
INNER JOIN Transaction_Info ON 
    Payment_Transaction.trans_info_id = Transaction_Info.trans_info_id 
GROUP BY ID_Number, student_lname, student_fname, trans_name;

You probably don't need all these NULLIF() 's, but since I'm not sure what column is causing issues it's better to take all. 您可能不需要所有这些NULLIF() ,但由于我不确定哪个列导致问题,所以最好采取所有。 If it works with this code, you can start removing the NULLIF from student names and trans_name . 如果它与此代码一起使用,您可以开始从学生姓名trans_name中删除NULLIF。

您需要定义第一个id列主键mvc在与模型一起使用时不允许这样做

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM