jon*_*hin 4 c# sql-server entity-framework-core
DB第一个实体框架核心在SaveChanges()时抛出此异常;此代码适用于 Entity Framework,但不适用于 Entity Framework Core。
异常消息:无法将类型“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; }
}
Run Code Online (Sandbox Code Playgroud)
-------------------------------------------------- ----------------------------
数据定义语言
创建表 CS_Product(
产品No bigint IDENTITY(1,1) NOT NULL,
产品名称 nvarchar(256) NOT NULL,
最便宜的价格小数(14,2) NOT NULL,
最后价格小数(14,2) NOT NULL,
数量 int NOT NULL,
上传日期日期时间不为空,
最后更新日期日期时间不为空,
评论 nvarchar(450) ,
CONSTRAINT PK_CS_Product PRIMARY KEY (productNo),
);
这是我的错误。当我第一次创建表时,productNo 是 bigint,我根据该表创建了模型。之后不知何故(一定是我......)productNo 更改为 int。更换产品后工作正常没有回到bigint
| 归档时间: |
|
| 查看次数: |
29145 次 |
| 最近记录: |