Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# System.Data.Entity.Core.EntityCommandExecutionException和内部异常SqlException:列名无效';xxxx和x27;_C#_Visual Studio_Entity Framework_Exception_Entity Framework 6 - Fatal编程技术网

C# System.Data.Entity.Core.EntityCommandExecutionException和内部异常SqlException:列名无效';xxxx和x27;

C# System.Data.Entity.Core.EntityCommandExecutionException和内部异常SqlException:列名无效';xxxx和x27;,c#,visual-studio,entity-framework,exception,entity-framework-6,C#,Visual Studio,Entity Framework,Exception,Entity Framework 6,这是关于尝试从数据库获取数据网格值时出现的异常 当我尝试运行应用程序时,我得到了下面提到的错误: System.Data.Entity.Core.EntityCommandExecutionException:执行命令定义时出错。有关详细信息,请参见内部异常 内部异常 SqlException:列名“AssetClass\u Id”无效 当从迁移更新数据库时,上述外键列AssetClassId已修改为AssetClassId。数据库在设计中也将其列值显示为AssetClassId 这是我的资产类

这是关于尝试从数据库获取数据网格值时出现的异常

当我尝试运行应用程序时,我得到了下面提到的错误:

System.Data.Entity.Core.EntityCommandExecutionException:执行命令定义时出错。有关详细信息,请参见内部异常

内部异常
SqlException:列名“AssetClass\u Id”无效

当从迁移更新数据库时,上述外键列
AssetClassId
已修改为
AssetClassId
。数据库在设计中也将其列值显示为
AssetClassId

这是我的
资产类型
类别代码:

    public int Id { get; set; }

    [Required]
    [StringLength(50)]
    public string Name { get; set; }

    [StringLength(100)]
    public string Description { get; set; }

    public AssetClass AssetClass { get; set; }
下面是我在form load方法下编写的.cs文件代码:

using (CapexDbContext db = new CapexDbContext())
{
    // Load data to grdAssetType
    assetTypeBindingSource.DataSource = db.AssetTypes.ToList();
    AssetType obj = assetTypeBindingSource.Current as AssetType;
    panelAssetType.Enabled = false;

    // Load data to cmbAssetClasses
    var assetClass_ = from AssetClass in db.AssetClasses
                      select AssetClass;

    cmbAssetClass.DataSource = assetClass_.ToList();
    cmbAssetClass.DisplayMember = "Name";
    cmbAssetClass.ValueMember = "Id";
}
尝试将数据提取到dataGrid时,此行出现异常:

assetTypeBindingSource.DataSource = db.AssetTypes.ToList();
此外,以下是我的最新迁移代码:

         CreateTable(
            "dbo.AssetClasses",
            c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    Code = c.String(nullable: false, maxLength: 10),
                    Name = c.String(nullable: false, maxLength: 100),
                    Lifetime = c.Int(nullable: false),
                    Rate = c.Single(nullable: false),
                })
            .PrimaryKey(t => t.Id);
        
        CreateTable(
            "dbo.AssetTypes",
            c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                    Name = c.String(nullable: false, maxLength: 50),
                    Description = c.String(maxLength: 100),
                    AssetClassId = c.Int(nullable: false),
                })
            .PrimaryKey(t => t.Id)
            .ForeignKey("dbo.AssetClasses", t => t.AssetClassId)
            .Index(t => t.AssetClassId);
运行数据库表:资产类型和资产类别(参考表)


尝试查看项目中的DBcontext,尝试查看它在实体定义中的含义,特别是在本部分:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);
    //modelBuilder.Entity<IdentityUser>().ToTable("user");
    
    
    modelBuilder.Entity<ApplicationUser>(entity =>
    {
        entity.ToTable("user");
        entity.Property(e => e.CreationAtDate)
        .HasColumnName("created_at") //<------ THIS ONE
        .HasDefaultValueSql("(getDate())");
    });
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
基于模型创建(modelBuilder);
//modelBuilder.Entity().ToTable(“用户”);
modelBuilder.Entity(Entity=>
{
实体。可转让(“用户”);
entity.Property(e=>e.creationadate)

.HasColumnName(“created_at”)/我注意到您的
AssetType
类包含
AssetClass
属性

同时,数据库中的表包含
AssetClassId
字段

他们是不同的

因此,我们应该在
AssetType
类中定义类

 public class AssetType
    {
        public int Id { get; set; }

        [Required]
        [StringLength(50)]
        public string Name { get; set; }

        [StringLength(100)]
        public string Description { get; set; }

        public int AssetClassID { get; set; }    // We need to modify it here
    }
然后,我们需要在PackageManager控制台中执行命令来更新数据库

PM> Add-Migration Modify

PM> Update-Database
最后,我们可以使用以下代码在datagirdview中显示数据

private void Form1_Load(object sender, EventArgs e)
        {
            using (var db = new MyContext())
            {

                var type = new AssetType { Id = 1001, Description = "test1", Name = "name1", AssetClassID=123456 };
                db.Types.Add(type);
                db.SaveChanges();
                dataGridView1.DataSource = db.AssetTypes.ToList();
              
            }
        }
结果:


可能您的数据库没有更新为您的模型。请使用
添加迁移
更新数据库
命令。我的DBContext没有任何onmodel创建方法……我想,即使进行迁移,您也可能不需要onmodel创建。您能在运行的数据库中共享表的屏幕截图吗?尝试在他说: