我使用Firebird作为我需要使用ASP.NET MVC 5开发b2b应用程序所需的数据库。在表之间使用外键是否有错误,是否可以添加必要的修复程序? Visual Studio提供的错误如下:EntityFramework.dll中出现“System.InvalidOperationException”类型的异常,但未在用户代码中处理。
在处理单个非相关表时,我没有遇到任何错误。我认为我错误地建立了表之间的关系。
DatabaseContext类,这个类数据库连接;
namespace InoturYolcuKayit.Models
{
[DbConfigurationType(typeof(FirebirdContextConfiguration))]
public class DatabaseContext : DbContext
{
public DatabaseContext()
: base(new FbConnection(@"database=D:\data\INOTUR.FDB;charset=WIN1254;user=sysdba;password=ino38sis;"), true)
{ }
public DbSet<PERSONEL> Personel { get; set; }
public DbSet<ROLES> Roles { get; set; }
}
}
FirebirdContextConfiguration类:
namespace InoturYolcuKayit.Models
{
class FirebirdContextConfiguration : DbConfiguration
{
public FirebirdContextConfiguration()
{
SetDatabaseInitializer<DatabaseContext>(null);
}
}
}
表中的PERSONEL.cs Firebird数据库;
public class PERSONEL
{
[Key, Required]
public int PERSONEL_NO { get; set; }
[Required, StringLength(30)]
public string ADI { get; set; }
public int ULKE_NO { get; set; }
[ForeignKey("ROLES")]
[Column("ROLE_ID")]
public int ROLE_ID { get; set; }
public ROLES ROLES { get; set; }
}
表中的ROLES.cs firebird数据库;
public class ROLES
{
public ROLES()
{
PERSONEL = new List<PERSONEL>();
}
[Key, Required]
public int ROLE_ID { get; set; }
[StringLength(30)]
public string ROLE_NAME { get; set; }
public virtual ICollection<PERSONEL> PERSONEL { get; set; }
}
我在一个简单的代码行中ROLES_ID
收到此错误,该代码列出PERSONELS
控制器中的索引viewda人员:
EntityFramework.dll中发生了'System.InvalidOperationException'类型的异常,但未在用户代码中处理。
public class PERSONELsController : Controller
{
private DatabaseContext db = new DatabaseContext();
// GET: PERSONELs
public ActionResult Index()
{
var personel = db.Personel.Include(p => p.ROLES);
return View(personel.ToList());
}
}
此错误堆栈跟踪;
[InvalidOperationException: Sequence does not contain matching items]
System.Linq.Enumerable.Single(IEnumerable`1 source, Func`2 predicate) +2643765
System.Data.Entity.Utilities.DbProviderManifestExtensions.GetStoreTypeFromName(DbProviderManifest providerManifest, String name) +81
System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.PrimitivePropertyConfiguration.ConfigureColumn(EdmProperty column, EntityType table, DbProviderManifest providerManifest) +87
System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.BinaryPropertyConfiguration.ConfigureColumn(EdmProperty column, EntityType table, DbProviderManifest providerManifest) +120
System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.PrimitivePropertyConfiguration.Configure(EdmProperty column, EntityType table, DbProviderManifest providerManifest, Boolean allowOverride, Boolean fillFromExistingConfiguration) +138
System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.<>c__DisplayClass4.<Configure>b__3(Tuple`2 pm) +38
System.Data.Entity.Utilities.IEnumerableExtensions.Each(IEnumerable`1 ts, Action`1 action) +130
System.Data.Entity.ModelConfiguration.Configuration.Properties.Primitive.PrimitivePropertyConfiguration.Configure(IEnumerable`1 propertyMappings, DbProviderManifest providerManifest, Boolean allowOverride, Boolean fillFromExistingConfiguration) +94
System.Data.Entity.ModelConfiguration.Configuration.Types.StructuralTypeConfiguration.ConfigurePropertyMappings(IList`1 propertyMappings, DbProviderManifest providerManifest, Boolean allowOverride) +154
System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.ConfigurePropertyMappings(DbDatabaseMapping databaseMapping, EntityType entityType, DbProviderManifest providerManifest, Boolean allowOverride) +585
System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.Configure(EntityType entityType, DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest) +177
System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.ConfigureEntityTypes(DbDatabaseMapping databaseMapping, ICollection`1 entitySets, DbProviderManifest providerManifest) +423
System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.Configure(DbDatabaseMapping databaseMapping, DbProviderManifest providerManifest) +376
System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo) +444
System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection) +55
System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext) +61
System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input) +123
System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +616
System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +18
System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +53
System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator() +16
System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator() +54
System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +375
System.Linq.Enumerable.ToList(IEnumerable`1 source) +58
hacumre.Controllers.HEDIYELERsController.Index() in C:\Users\Abdullah\source\repos\hacumre\hacumre\Controllers\HEDIYELERsController.cs:21
lambda_method(Closure , ControllerBase , Object[] ) +61
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +157
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +27
System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +22
System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) +29
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32
System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +50
System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +228
System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34
System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +26
System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +100
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +36
System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +12
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +22
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9987157
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155
答案 0 :(得分:0)
在PERSONNEL类中,您在属性之间反转了DataAnnotations,而Roles
属性不是虚拟的,请使用:
[Column("ROLE_ID")] // facultative cause the property has the same name
public int ROLE_ID { get; set; }
[ForeignKey("ROLE_ID ")]
public virtual ROLES ROLES { get; set; }