如何修复C#.NET实体框架核心模型中的SQLite错误1?
我正在开发一个控制台应用程序,它利用了名为Entity framework Core的C#/.NET框架,该框架与SQLite数据库语言一起工作。到目前为止,这个应用程序应该做的是输出假冒公司拥有的所有产品类别以及每个类别中的产品数量 下面是输出应该是什么样子的:如何修复C#.NET实体框架核心模型中的SQLite错误1?,c#,.net,entity-framework,sqlite,unix,C#,.net,Entity Framework,Sqlite,Unix,我正在开发一个控制台应用程序,它利用了名为Entity framework Core的C#/.NET框架,该框架与SQLite数据库语言一起工作。到目前为止,这个应用程序应该做的是输出假冒公司拥有的所有产品类别以及每个类别中的产品数量 下面是输出应该是什么样子的: Categories and how many products they have: Beverages has 12 products. Condiments has 12 products. Confections has 13
Categories and how many products they have:
Beverages has 12 products.
Condiments has 12 products.
Confections has 13 products.
Dairy Products has 10 products.
Grains/Cereals has 7 products.
Meat/Poultry has 6 products.
Produce has 5 products.
Seafood has 12 products.
但是,无论何时运行应用程序,都会得到以下输出:
Categories and how many products they have:
Unhandled exception. Microsoft.Data.Sqlite.SqliteException (0x80004005): SQLite Error 1: 'no such column: c.Products.Discontinuted'.
at Microsoft.Data.Sqlite.SqliteException.ThrowExceptionForRC(Int32 rc, sqlite3 db)
at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader(CommandBehavior behavior)
at Microsoft.Data.Sqlite.SqliteCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.ExecuteReader()
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues)
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteReader(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.BufferlessMoveNext(Boolean buffer)
at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.<>c__DisplayClass12_0`2.<Execute>b__0(DbContext c, TState s)
at Microsoft.EntityFrameworkCore.Storage.Internal.NoopExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute[TState,TResult](IExecutionStrategy strategy, Func`2 operation, Func`2 verifySucceeded, TState state)
at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute[TState,TResult](IExecutionStrategy strategy, TState state, Func`2 operation)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.MoveNext()
at Microsoft.EntityFrameworkCore.Query.Internal.QueryBuffer.IncludeCollection(Int32 includeId, INavigation navigation, INavigation inverseNavigation, IEntityType targetEntityType, IClrCollectionAccessor clrCollectionAccessor, IClrPropertySetter inverseClrPropertySetter, Boolean tracking, Object entity, Func`1 relatedEntitiesFactory)
at lambda_method(Closure , QueryContext , Category , Object[] )
at Microsoft.EntityFrameworkCore.Query.Internal.IncludeCompiler._Include[TEntity](QueryContext queryContext, TEntity entity, Object[] included, Action`3 fixup)
at System.Linq.Enumerable.SelectEnumerableIterator`2.MoveNext()
at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider._TrackEntities[TOut,TIn](IEnumerable`1 results, QueryContext queryContext, IList`1 entityTrackingInfos, IList`1 entityAccessors)+MoveNext()
at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
at WorkingWithEFCore.Program.QueryingCategories() in /Users/steelwind/HardWay/c#and.NET/Chapter11/WorkingWithEFCore/Program.cs:line 24
at WorkingWithEFCore.Program.Main(String[] args) in /Users/steelwind/HardWay/c#and.NET/Chapter11/WorkingWithEFCore/Program.cs:line 12
下面是连接到数据库的代码:
using Microsoft.EntityFrameworkCore;
namespace Packt.CS7
{
// this manages the connect to the database
public class Northwind : DbContext
{
// these properties map to tables in the database
public DbSet<Category> Categories { get; set; }
public DbSet<Product> Products { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// to use SQLite, uncomment the following
string path = System.IO.Path.Combine(
System.Environment.CurrentDirectory, "Northwind.db");
optionsBuilder.UseSqlite($"Filename={path}");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// example of using Fluent API instead of attributes
// to limit the length of a category name to under 40
modelBuilder.Entity<Category>()
.Property(category => category.CategoryName)
.IsRequired()
.HasMaxLength(40);
}
}
}
使用Microsoft.EntityFrameworkCore;
命名空间Packt.CS7
{
//这将管理与数据库的连接
公共类Northwind:DbContext
{
//这些属性映射到数据库中的表
公共数据库集类别{get;set;}
公共数据库集产品{get;set;}
配置时受保护的覆盖无效(DBContextOptions Builder Options Builder)
{
//要使用SQLite,请取消注释以下内容
字符串路径=System.IO.path.Combine(
System.Environment.CurrentDirectory,“Northwind.db”);
optionsBuilder.UseSqlite($“Filename={path}”);
}
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
//使用Fluent API代替属性的示例
//将类别名称的长度限制在40以下
modelBuilder.Entity()
.Property(category=>category.CategoryName)
.IsRequired()
.HasMaxLength(40);
}
}
}
它表示您的表中没有列(c.Products.discontinued
)。我发现问题,Products.cs文件的第20行有一个打字错误。
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
namespace Packt.CS7
{
public class Category
{
// these properties map to columns in the database
public int CategoryID { get; set; }
public string CategoryName { get; set; }
[Column(TypeName = "ntext")]
public string Description { get; set; }
// defines a navigation property for related rows
public virtual ICollection<Product> Products { get; set; }
public Category()
{
// to enable developers to add products to a Category we must
// initiate the navigation property to an empty list
this.Products = new List<Product>();
}
}
}
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Packt.CS7
{
public class Product
{
public int ProductID { get; set; }
[Required]
[StringLength(40)]
public string ProductName { get; set; }
[Column("UnitPrice", TypeName = "money")]
public decimal? Cost { get; set; }
[Column("UnitsInStock")]
public short? Stock { get; set; }
public bool Discontinuted { get; set; }
// these two define the foriegn key relationship
// to the Category table
public int CategoryID { get; set; }
public virtual Category Category { get; set; }
}
}
using Microsoft.EntityFrameworkCore;
namespace Packt.CS7
{
// this manages the connect to the database
public class Northwind : DbContext
{
// these properties map to tables in the database
public DbSet<Category> Categories { get; set; }
public DbSet<Product> Products { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
// to use SQLite, uncomment the following
string path = System.IO.Path.Combine(
System.Environment.CurrentDirectory, "Northwind.db");
optionsBuilder.UseSqlite($"Filename={path}");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// example of using Fluent API instead of attributes
// to limit the length of a category name to under 40
modelBuilder.Entity<Category>()
.Property(category => category.CategoryName)
.IsRequired()
.HasMaxLength(40);
}
}
}