希望大家都好,我试图通过" LINQ"将新记录插入数据库。并且有一个问题!!
型号:
[Table("Rate")]
public class Item_rate
{
[Key]
public int Id { get; set; }
public int Up { get; set; }
public int Down { get; set; }
public Item_rate(int t, int u, int d)
{
Id = t;
Up = u;
Down = d;
}
public Item_rate(int t) { Id = t; }
public Item_rate() { Id = 1; }
}
public class RRDBContext : DbContext
{
public DbSet<Item_rate> itrat { get; set; }
}
数据库:
表=比率
Pk = Id - &gt; int
UP - &gt; int
向下 - &gt; int
//这些都不允许空值
控制器:
Item_rate f = new Item_rate(y);
f.Id = y;
f.Down = 0;
f.Up = 0;
ratdb.itrat.Add(f);
ratdb.SaveChanges(); //HERE IS THE EXCEPTION
在Web.config中:
add name="RRDBContext " connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=C:\Users\Admin\Desktop\Recommender.mdf;Integrated Security=True" providerName="System.Data.SqlClient"
例外:
{&#34;无法将值NULL插入列&#39; Id&#39;,table&#39; C:\ USERS \ ADMIN \ DESKTOP \ RECOMMENDER.MDF.dbo.Rate&#39;列不允许空值。 INSERT失败。\ r \ n语句已终止。&#34;}