DB第一個實體框架核心在SaveChanges(); 此代碼適用於實體框架,但不適用於實體框架核心。Entityframework Core無法將類型爲'System.Int32'的對象強制類型爲'System.Int64'
異常消息:無法將類型爲「System.Int32」的對象轉換爲鍵入「System.Int64」。
CsProduct csProduct = new CsProduct()
{
CheapestPrice = 1,
LastPrice = 1,
LastUpdateDate = DateTime.Now,
ProductName = "123",
Quantity = 1,
UploadDate = DateTime.Now
};
CSContext ctx = new CSContext();
ctx.Entry(csProduct).State = EntityState.Added;
// ctx.CsProduct.Add(csProduct); This does not work neither
csProduct.ProductNo = 0; // Still throws exception without this line
ctx.SaveChanges();
------------------------------------------------------------------
CsProduct.cs
public partial class CsProduct
{
public CsProduct()
{
}
public long ProductNo { get; set; }
public string ProductName { get; set; }
public decimal CheapestPrice { get; set; }
public decimal LastPrice { get; set; }
public int Quantity { get; set; }
public DateTime UploadDate { get; set; }
public DateTime LastUpdateDate { get; set; }
public string Comment { get; set; }
}
------------------------------------------------------------------------------ DDL CREATE TABLE CS_Product( productNo bigint IDENTITY(1,1) NOT NULL, productName nvarchar(256) NOT NULL, cheapestPrice decimal(14,2) NOT NULL, lastPrice decimal(14,2) NOT NULL, quantity int NOT NULL, uploadDate datetime NOT NULL, lastUpdateDate datetime NOT NULL, comment nvarchar(450) , CONSTRAINT PK_CS_Product PRIMARY KEY (productNo), );
是否有任何流利的映射代碼或錯誤地將類型設置爲'Int32'的遷移代碼? – Igor
EF Core版本:1.1.0,它應該正確映射主鍵。因爲它是由實體框架使用Scaffold-DbContext生成的 –