[英]C# Entity Framework and DB - change from smallint to int
Due to a certain change we have to make, some columns in the db have to be changed from smallint to int and as a result, we have to change the code variables from short to int .由于我们必须进行某些更改,db 中的某些列必须从smallint更改为int ,因此,我们必须将代码变量从short更改为int 。
Since it is a pretty big change, we would like to do it in 2 steps - first change the db and only then change the code (or the other way around).由于这是一个相当大的更改,我们希望分两步完成 - 首先更改数据库,然后更改代码(或相反)。
We did a small test - changed a column in a specific table from smallint to int without changing the code - the code variable is still expecting short .我们做了一个小测试——在不改变代码的情况下将特定表中的一列从smallint更改为int —— code 变量仍然期望short 。
When using EF to get results from the DB and assigning it to the appropriate c# class, we get the following error (which is reasonable):当使用 EF 从 DB 获取结果并将其分配给适当的 c# 类时,我们得到以下错误(这是合理的):
"System.InvalidOperationException: The 'CountryID' property on 'TestClass' could not be set to a 'System.Int32' value. You must set this property to a non-null value of type 'System.Int16'
“System.InvalidOperationException:“TestClass”上的“CountryID”属性无法设置为“System.Int32”值。您必须将此属性设置为“System.Int16”类型的非空值
EDIT: similar exception raises if we first try to change the code variable to be int and working with db column as smallint :编辑:如果我们首先尝试将代码变量更改为int并使用 db 列作为smallint ,则会引发类似的异常:
"System.InvalidOperationException: The 'CountryID' property on 'TestClass' could not be set to a 'System.Int16' value. You must set this property to a non-null value of type 'System.Int32'
“System.InvalidOperationException:“TestClass”上的“CountryID”属性无法设置为“System.Int16”值。您必须将此属性设置为“System.Int32”类型的非空值
Some code and data:一些代码和数据:
CountryID (int, not null)
.CountryID (int, not null)
。public class TestClass
{
public short CountryID { get; set; }
}
the modelBuilder is:模型构建器是:
ToTable("Country");
// mapping an int column in the db (CountryID) to a short variable in the code (CountryID)
Property(t => t.CountryID).HasColumnName("CountryID").IsRequired();
Trying to query the table with EF:尝试使用 EF 查询表:
var listOfCountryIDs = dbContext.TestEntity.Where(t => t.CountryID == countryID)
, raises the exception. var listOfCountryIDs = dbContext.TestEntity.Where(t => t.CountryID == countryID)
,引发异常。
We tried to force the model builder with adding .HasColumnType("smallint")
but it didn't help.我们试图通过添加
.HasColumnType("smallint")
来强制模型构建器,但它没有帮助。 Also tried this .也试过这个。
Of course it works if we change both the code and the db to work with ints (or short & smallint).当然,如果我们同时更改代码和数据库以使用整数(或 short 和 smallint),它就可以工作。
Is there any way to apply this change in 2 steps?有什么方法可以分两步应用此更改吗? Any help would be appreciated, thanks.
任何帮助将不胜感激,谢谢。
I am afraid,you need to change datatype both at DB and Model level simultaneously.恐怕,您需要同时在数据库和模型级别更改数据类型。 Reason is why you getting the exception mentioned in the questions is, entityframework trying to put 4 byte data from DB into 2 byte of Model, since no direct casting of this type is available hence you facing it.
原因是您收到问题中提到的异常的原因是,entityframework 试图将 4 字节数据从 DB 放入 2 字节模型,因为这种类型的直接转换不可用,因此您面临它。
As you mentioned that this is very large change, I suggest you to go one DB/One model at a time.正如您提到的,这是一个非常大的变化,我建议您一次使用一个 DB/One 模型。
thanks谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.