MVC-EF逆向工程师代码首先包括另外一个表返回null,当有数据时 [英] MVC-EF Reverse Engineer Code first include another table returns nulls when there should be data

查看:196
本文介绍了MVC-EF逆向工程师代码首先包括另外一个表返回null,当有数据时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有麻烦的.Include()的语法,这个相关的问题让我到它编译和运行的点:

I was having trouble with the syntax for the .Include() and this related question got me to the point where it both compiled and ran:

MVC-EF逆向工程师代码首先包括另一个表

我使用:

 [ForeignKey("Id")]    
 public OtherTableModel OtherTableModel {get; set;}

两个表都具有完全相同的主键:

Both tables have the exact same primary key:

 string id {get; set} 

在我的控制器中,代码是:

In my controller the code is:

ourList = db.Table1.Include(t => t.OtherTableModel)

不幸的是,尽管这在编译甚至运行方面起作用,但有时候,当包含有效数据时,Include语句的结果将为空。用作外键的Id使得在实际的SQL服务器中将不是指定为外键,尽管相同的字符串将是该表的主键。这个SQL问题可能是问题的一部分。

Unfortunately, though this works in the sense of compiling and even running, sometimes the results from the "Include" statement are null when there should be valid data. The "Id" used as a "foreign key" is such that in the actual SQL server it is not specified as a foreign key, though the same string will be the primary key to that table. This SQL issue may be part of the problem.

当我使用MVC调用的存储过程检索其他表数据时,它的工作原理与SQL查询直接相同在SSMS中(即在 行中有有效的数据,而不是null),但是include包含一些有效的数据行,但不是所有的数据行不明白,我会认为,始终工作或始终失败,但是从最初发布以来,我发现了更多的见解,请参见下文)。那里的表中的可能的数据行,其中null将是有效的,但是,当不应该返回的数据为空时,有很多次。自从最初发布以来,我已经发现:

When I use a stored procedure invoked by MVC to retrieve the other table data, it works, as does a SQL query directly in SSMS (i.e. there are valid data in all rows, and not nulls) but the include returns some valid data rows, but not all of the data rows (which I really don't understand at all, I'd think it would always work or always fail, however since originally posting I've found more insight, please see below). There are possible data rows in the table where a null would be valid, however, there are many times when the data returned is null when it shouldn't be. Since originally posting, I have found this out:

在这种情况下,密钥id [两个表]的字符串的长度限制为最多8个字符最大)。当它正好是8个字符时,来自另一个表的.include加载是正确的,当它不是8个字符(即小于8个字符)时,返回null。也许请有人可以详细说明为什么这是和建议修复。假设桌子不能改变,我们必须住不同长度的身份证件。

In this case, the string for the key id [both tables] is limited in length to up to 8 characters (i.e. a maximum). When it is exactly 8 characters, the .include loading from the other table is correct, when it is not exactly 8 characters (i.e. less than 8), a null is returned. Perhaps please someone could elaborate on why this is and suggest a fix. Assume that the table cannot be changed and we have to live with id's of varying lengths, please.

有人有任何见解吗?

提前感谢!

推荐答案

如果您的数据库列是 char nchar ,这可能是问题。在SQL Server中, char nchar 将用空格右键填充数据。所以如果你的值是1234567,数据库中存储的实际值是1234567(注意尾随空格)。这是假设一个 char(8) nchar(8)

If your database column(s) are char or nchar, that may be the issue. In SQL Server, both char and nchar will right-pad the data with whitespace. So if your value is "1234567", the actual value stored in the database is "1234567 " (note the trailing space). This is assuming a char(8) or nchar(8).

对于列数据类型 varchar nvarchar ,不会发生填充。

For column data types varchar and nvarchar, no padding will occur.

这篇关于MVC-EF逆向工程师代码首先包括另外一个表返回null,当有数据时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆